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??

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Esaïe Njongssi
  • 63
  • 2
  • 13
  • 1
    What is the value of `$eip`? It appears that you stopped _before_ the first `push`. – Employed Russian May 08 '21 at 23:08
  • `0xf0100d81` is the value of `eip` – Esaïe Njongssi May 09 '21 at 07:47
  • `info registers` and x/x $edi + 0x1fb0. Are $eax and that memory location 0? – mevets May 10 '21 at 18:03
  • Eax has non zero value – Esaïe Njongssi May 14 '21 at 16:06
  • 1
    I would run QEMU with options `-no-shutdown -no-reboot -d int`. The `-d int` being msot important because it will dump information out for every interrupt/exception raised. `v=` entries are the vector number and the `e=` value beside it is important as well as it is the error number that can give you an indication what the problem is. – Michael Petch May 14 '21 at 21:34
  • Do you have a project online somewhere like Github? – Michael Petch May 15 '21 at 18:11
  • @MichaelPetch ,[there is the github link](https://pdos.csail.mit.edu/6.828/2018/jos.git) it mostly for educationnal course for https://pdos.csail.mit.edu/6.828/2018/labs/lab2/ – Esaïe Njongssi May 18 '21 at 08:41
  • Yeah but do you have a project with your own changes in it so I can toss it in the debugger? – Michael Petch May 18 '21 at 18:06
  • thank you very much :) @MichaelPetch , there is the link [project with my own change](https://github.com/kouamdo/JOS_kernel/blob/32c7ffae687db0297e233d562899a64c8575a585/kern/pmap.c#L172) i'm trying to resolve this [part](https://pdos.csail.mit.edu/6.828/2018/labs/lab2/#Exercise-1) , and there is the [main](https://github.com/kouamdo/JOS_kernel/blob/32c7ffae687db0297e233d562899a64c8575a585/kern/init.c#L30) – Esaïe Njongssi May 19 '21 at 05:03
  • type `make qemu-gdb` in the terminal and open another terminal type `make gdb` to debug it – Esaïe Njongssi May 19 '21 at 05:40

1 Answers1

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.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
  • I'm in kernel mode sir – Esaïe Njongssi May 14 '21 at 16:05
  • do you have info about the kernel panic? – Luis Colorado May 14 '21 at 19:20
  • 1
    @EsaïeNjongssi: Are you hoping that `cli` will stop a #PF exception from being raised if memset tries to write memory that the page tables don't map? As Luis explains, disabling interrupts only blocks *external* interrupts, not synchronous exceptions / faults that are triggered by running instructions. So don't pass a bad address to memset, and/or check your page tables. (BOCHS's built-in debugger can decode page tables so you can check that they're set up the way you intended.) – Peter Cordes May 15 '21 at 01:52