6.828-Hw1-Boot xv6
Homework: boot xv6
Exercise: What is on the stack
Begin by restarting qemu and gdb, and set a break-point at 0x7c00, the start of the boot block (bootasm.S). Single step through the instructions (type si
at the gdb prompt). Where in bootasm.S is the stack pointer initialized? (Single step until you see an instruction that moves a value into %esp
, the register for the stack pointer.)
It’s at
1 | # Set up the stack pointer and call into C. |
Single step through the call to bootmain
; what is on the stack now?
Before we call bootmain
, ESP still points where the boot loader starts.
1 | (gdb) b*0x7c48 |
Then step into bootmain
. Now the first and only element on stack is 0x00007c4d
. From bootblock.asm
, we can know that This is where the bootmain
returns.
1 | (gdb) si |
1 | # If bootmain returns (it shouldn't), trigger a Bochs |
This shows call
will also modify %ESP
, pushing the return address into the stack.
Continue to run till 0x7d30
1 | (gdb) si |
That corresponds to this asm code
1 | 7d2a: 55 push %ebp |
We can know that the stack are like this in memory:
1 | VALUE ADDRESS |
What do the first assembly instructions of bootmain do to the stack? Look for bootmain in bootblock.asm.
1 | => 0x7d2a: push %ebp |
Push %ebp
into the stack
Continue tracing via gdb (using breakpoints if necessary – see hint below) and look for the call that changes eip
to 0x10000c. What does that call do to the stack? (Hint: Think about what this call is trying to accomplish in the boot sequence and try to identify this point in bootmain.c, and the corresponding instruction in the bootmain code in bootblock.asm. This might help you set suitable breakpoints to speed things up.)
It’s at 0x7db2
1 | => 0x7db2: call *0x10018 |
In bootmain.c
it’s entry()
1 | elf = (struct elfhdr*)0x10000; // scratch space |
From elf.h
we can know How did we get 0x10018
1 | // File header |
0x10000
+ 4 + C + 2 + 2 + 4 = 0x10018
What on 0x10018
is the destination of call
, also the value to be set on %eip
1 | (gdb) x/x 0x10018 |
That’s why next step we are at 0x10000c
From previous question, we’ve already noticed that call
will also modify %ESP
, pushing the return address into the stack.
6.828-Hw1-Boot xv6