How can i know that i have The Stack-Segment Fault somewhere?

0

It is my first time to see that when I push a value on the stack, the stack is still empty.

I have thought maybe it is the kernel panic, so that i have added cli ; cld before calling the function but nothing.

The example is when I want to call function, there the function is called memset :

eflags         0x6                 [ IOPL=0 PF ]  
│   0xf0100d6a <mem_init+172>       cli                                                                                                                                 │
│   0xf0100d6b <mem_init+173>       cld                                                                                                                                 │
│   0xf0100d6c <mem_init+174>       sub    esp,0x4                                                                                                                      │
│   0xf0100d6f <mem_init+177>       mov    eax,DWORD PTR [edi+0x1fb8]                                                                                                   │
│   0xf0100d75 <mem_init+183>       shl    eax,0x3                                                                                                                      │
│   0xf0100d78 <mem_init+186>       push   eax                                                                                                                          │
│   0xf0100d79 <mem_init+187>       push   0x0                                                                                                                          │
│   0xf0100d7b <mem_init+189>       push   DWORD PTR [edi+0x1fb0]                                                                                                       │
│  >0xf0100d81 <mem_init+195>       call   0xf010234b <memset>  

         

    (gdb) p $esp
$1 = (void *) 0xf00d1f8c
(gdb) stepi
=> 0xf0100d78 <mem_init+186>:   push   eax
=> 0xf0100d79 <mem_init+187>:   push   0x0
=> 0xf0100d7b <mem_init+189>:   push   DWORD PTR [edi+0x1fb0]
=> 0xf0100d81 <mem_init+195>:   call   0xf010234b <memset>
(gdb) p $esp
$2 = (void *) 0xf00d1f80
(gdb) x/4w $esp
0xf00d1f80:     0x00000000      0x00000000      0x00000000      0x00000000
(gdb) 

there is the C code :

asm("cli ; cld") ;
    
memset((void*) pages , 0 , (size_t)(sizeof(*pages)*npages) );

Can you tell me where I have made mistake ?? Is it Stack-Segment Fault that occurs??

x86
kernel
osdev
asked on Stack Overflow May 8, 2021 by Esaïe Njongssi • edited May 15, 2021 by Michael Petch

1 Answer

2

You are executing a privileged instruction. I don't know if you are in kernel mode or in user mode, but if you are in user mode, this generates immediately a trap.

EDIT

From the comments you say that you are in kernel mode, so mi next comment is about cli instruction will not inhibit a trap, but only hardware interrupt line, and not the traps caused by a page fault or an instruction error, a segmentation violation, access to unallocated memory, etc. The traps are errors (synchronous) caused by bad instructions, so the cpu cannot continue ignoring them, even if the interrupt flag is inhibiting interruptions, so the trap is jumped anyway. The most probable thing is that you are writing memory unallocated inside memset (marked in the pagetables as not usable) so double check the pointer pages and the segment covering pages ... pages + npages * sizeof *pages.

answered on Stack Overflow May 12, 2021 by Luis Colorado • edited May 14, 2021 by Luis Colorado

User contributions licensed under CC BY-SA 3.0