MIT6828 lab 1
Part 1
First thing, what does it mean by saying cs:ip
?
This is a old but still being used memory addressing method. Segment: Offfset
. The absolute memory addres = (Segment * 16) + Offset. For example, F000:FFFD
means address FFFFD
. This strange way of expressing address is due to the fact that in the old days, CPU’s regeisters are only 16bit, which can only address a memeory address of 2^16 (which is 64KiB memory). This is even too small at that age. So CPU manufacturers combines two regiesters to express larger address, which is this segment: offset
thing.cs
means “code segment” and ip
means “instruction pointer”. The combination represents the location where CPU is currently fetching instructions to execute.
1 | /\/\/\/\/\/\/\/\/\/\ |
Exercise 2: debug boot
1 | [CS:IP] address: command args |
Part 2 Boot Loader
BIOS handle control to boot loader by loading boot loader into memory and jump instruction pointer to 0x7C00
, where boot
loader code started.
Exercise 3
- At what point does the processor start executing 32-bit code? What exactly causes the switch from 16- to 32-bit mode?
[ 0:7c1e] => 0x7c1e: lgdtw 0x7c64
. Here starting using GDT, Global Descriptor Table.
Then mark control regesiter, cr0
.
1 | [ 0:7c23] => 0x7c23: mov %cr0,%eax |
In boot.S
source code, one can see .code16
as a sign of 16-bit mode, and .code32
as 32-bit mode.
What is the last instruction of the boot loader executed, and what is the first instruction of the kernel it just loaded?
1
2
3
4
5=> 0x7c40: mov $0x7c00,%esp # setup stack pointer to call boot/main.c
=> 0x7c45: call 0x7d15
=> 0x7d15: push %ebp # here we move to `main.c`. boot.S hands over control to main.c
=> 0x7d6b: call *0x10018 # this is the last instruction from boot loader
=> 0x10000c: movw $0x1234,0x472 # this is the first instruction from the kernel.Where is the first instruction of the kernel?
0x10000c
How does the boot loader decide how many sectors it must read in order to fetch the entire kernel from disk? Where does it find this information? The boot loader find sector number from
Notes:x/i $pc
: show current instruction (content that program counter, pc
, pointed to)si
: execute current instructionc
: execute until next break pointb *address
: set a break point at address
Exercise 4
- Understand
pointer.c
. Read K&R Ch5.1 - 5.5.
Note:
- Unary operators like * and ++ associate right to left.
++*p
and(*p)++
and*p++
. - (int *) ((char *) c) makes the trick, char is 1 byte long whereas int is 4 bytes.
Exercise 5
The first instruction that would break will be ljmp $PORT_MODE_CSEG, $protcseg
, $protcseg
is part of
.text segment, and the segement is assumed to be loaded at 0x7c00
.
Exercise 6
Breakpoint 0x7c00
1 | [ 0:7c00] => 0x7c00: cli |
Part 3: The Kernel
Exercise 7
this bit code activate page:
1 | # Turn on paging. |
Set the breakpoint at 0x00100025 (this is the pysical address of movl %eax, %cr0). We can see:
1 | (gdb) x/8x 0x00100000 |
After execute this line, we can see that both 100000 and f0100000 hold same content.
Memory is mapped.
1 | (gdb) x/8x 0x00100000 |
The next instruction need the virtual memeory setup, otherwise it cannot find the right memory address.
0xf010002f.
f0100028: b8 2f 00 10 f0 mov $0xf010002f,%eax
Exercise 8
The code is similar to unsigned int, just change base to 8:
1 | num = getuint(&ap, lflag); |
Explain the interface between printf.c and console.c. Specifically, what function does console.c export?
How is this function used by printf.c?
putch()
function in printf.c calls cputchar()
in console.c to show chars in concole.
the following code will give a new page when screen is full.
1 | if (crt_pos >= CRT_SIZE) { |
Exercies 9: The Stack
Determine where the kernel initializes its stack, and exactly where in memory its stack is located. How does the kernel reserve space for its stack? And at which “end” of this reserved area is the stack pointer initialized to point to?
The kernel initialized stack pointer in line 76 of kern/entry.S
with movl $(bootstacktop), %esp
.
By looking at obj/kern/kernel.asm
line 57, we know that stack pointer is pointing to 0xf0110000
which is right after the 1MB kernel space.
f0100034: bc 00 00 11 f0 mov $0xf0110000,%esp
Exercise 10
1 | int |