Download presentation
Presentation is loading. Please wait.
1
Booting and Calling Convention
CS 444/544 OS II Lab Session #2 Booting and Calling Convention
2
Lab Setup Check Use any machines in the following:
flip1.engr.oregonstate.edu flip2.engr.oregonstate.edu flip3.engr.oregonstate.edu os2.engr.oregonstate.edu
3
Today’s Topic Following the Booting Sequence
Stack and Calling Convention
4
Exercise 3
5
Exercise 3: How? Use tmux, open boot/boot.S and gdb at the same time..
Use ni,si, breakpoint
6
Exercise 3: Enabling Protected Mode
PROT_MODE_CSEG 0x8 Bootmain() is in boot/main.c
7
Exercise 3: bootmain In boot/main.c
8
Exercise 4-6 Ex 4: Read about ELF header and ELF file..
Ex 5: Use si, ni to follow the instructions after changing 0x7c00 to others, e.g., 0x6b00 or something.. Ex 6: gdb commands
9
GDB Command for Reading Memory
x100/wx [address or register] Examine 100 values sized as word (w, 4 bytes) b – byte g – 8 bytes In hexadecimal (x) d - decimal
10
Exercise 7: Virtual Memory
0xf == KERNBASE Virtual address 0xf ~ 0xffffffff Access physical address at (Virtual address – KERNBASE) E.g., 0xf > 0x123456 0xf > 0x1
11
Exercise 8 Read lib/printfmt.c, for vprintfmt()
Look at cases ‘x’ and ‘u’ as an example of hexadecimal and decimal Implement the case ‘o’ Similar to ‘x’ and ‘u’ It’s easy…
12
Exercise 9 ~ 11: Stack Backtrace
Must understand how stack works in x86..
13
Function call in x86 In kern/init.c
test_backtrace(5) -> test_backtrace(4) -> test_backtrace(3) -> 2 -> 1 -> mon_backtrace(0,0,0)… How this recursion can work in x86 computer?
14
x86 Stack All local variables are stored in the stack.
MY_ARG2 x86 Stack MY_ARG1 Return Addr Saved EBP All local variables are stored in the stack. A function call creates a new stack Start with esp, ends with ebp Grows downward! Push(A), subtract 4 from esp and store A to there… Pop, get the value at esp and add 4 to esp %ebp Local A ebp-8 Local B ebp-c Local C ebp-10 ARG 2 esp+4 ARG 1 esp %esp
15
Function call example my_function(MY_ARG1, MY_ARG2) { } int A; int B;
Return Addr Saved EBP my_function(MY_ARG1, MY_ARG2) { int A; int B; int C; other_function(ARG1, ARG2) } %ebp Local A ebp-8 Local B ebp-c Local C ebp-10 ARG 2 esp+4 ARG 1 esp %esp
16
How x86 manages stack? Let’s debug calling test_backtrace
Set the breakpoint at *i386_init
17
How x86 manages stack? Examine instructions… MY_ARG1 (5) %esp
18
How x86 manages stack? Call Push addr of next instr. Jump to target.
To return to there after func(). Jump to target. MY_ARG1 (5) %esp Return Addr 0xf01000f4 %esp
19
How x86 manages stack? In test backtrace MY_ARG1 (5) Return Addr
0xf01000f4 %esp Saved EBP %ebp %esp %ebp Saved ESI %esp Saved EBX %esp
20
How x86 manages stack? Call test_backtrace(x-1) MY_ARG1 (5)
Return Addr 0xf01000f4 Saved EBP %ebp Saved ESI Saved EBX %esp ARG 1 = x-1 == 4 %esp
21
How x86 manages stack? MY_ARG1 (5) Return Addr 0xf01000f4 Saved EBP
Saved ESI Saved EBX %esp ARG 1 = 4 Return Addr 0xf01000a1 Saved EBP %ebp Saved ESI Saved EBX %esp Do this until the value reaches 0…
22
How x86 manages stack? ret == pop %eip MY_ARG1 (5) Return Addr
0xf01000f4 Saved EBP %ebp Saved ESI ret == pop %eip Saved EBX %esp ARG 1 = 4 Return Addr 0xf01000a1 %esp Saved EBP %esp %ebp Saved ESI %esp Saved EBX %esp
23
How x86 manages stack? Call test_backtrace(x-1) MY_ARG1 (5)
Return Addr 0xf01000f4 Saved EBP %ebp Saved ESI Saved EBX ARG 1 = x-1 %esp
24
x86 Stack ebp points the boundary of the stack (bottom)
esp points to the other boundary of the stack (top) ebp[0] stores saved ebp ebp[1] stores return address ebp[2] stores 1st argument ebp[3] stores 2nd argument …
25
Hint – Exercise 11 int *ebp = (int *) read_ebp();
cprintf(“ebp %08x”, ebp)… EIP == return address ebp[1] – why? Args? Print ebp[2 ~ 6]… … ebp[3] MY_ARG1 (5) ebp[2] Return Addr 0xf01000f4 ebp[1] Saved EBP ebp[0] %ebp Saved ESI Saved EBX ARG 1 = x-1 %esp
26
Coding Convention (CODING)
No space after a function name in a call cprintf(“asdf”) GOOD cprintf (“asdf”) NO One space after if/for/while/switch if (a == 1) { GOOD if(a==1) { NO function_and_variable_names_look_like_this NoCamelCase Macros are ALL UPPERCASE e.g., SEG()
27
Coding Convention (CODING)
Pointer types includes a space before * (uint32_t *) GOOD (uint32_t*) NO Use ‘//’ for your comment All imported comments are /**/, so we can distinguish yours from those FYI, Linux Kernel uses /**/… Function with no args f(void), not f();
28
Coding Convention (CODING)
Function definition Insert newline between the return type and function name This will make finding function definition easy E.g., find the definition of mon_kerninfo would be: ‘^mon_kerninfo’ in regexp.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.