OS lab1

The help command is obvious, and we will shortly discuss the meaning of what the kerninfo command prints.
Although simple, it's important to note that this kernel monitor is running "directly" on the "raw (virtual) hardware" of the simulated PC.
This means that you should be able to copy the contents of obj/kern/kernel.img onto the first few sectors of a real hard disk, insert that hard disk into a real PC, turn it on, and see exactly the same thing on the PC's real screen as you did above in the QEMU window.
(We don't recommend you do this on a real machine with useful information on its hard disk, though, because copying kernel.img onto the beginning of its hard disk will trash the master boot record and the beginning of the first partition, effectively causing everything previously on the hard disk to be lost!)

Ex1 读手册 #TODO
Ex2 配环境、使用help和kerninfo命令
Ex3
The boot loader consists of one assembly language source file, boot/boot.S, and one C source file, boot/main.c
First, the boot loader switches the processor from real mode to 32-bit protected mode
Second, the boot loader reads the kernel from the hard disk by directly accessing the IDE disk device registers via the x86's special I/O instructions

  1. At what point does the processor start executing 32-bit code? What exactly causes the switch from 16- to 32-bit mode?
    jmp PROT_MODE_CSEG,protcseg
  2. What is the last instruction of the boot loader executed, and what is the first instruction of the kernel it just loaded?
    ELFHDR->e_entry
    movw $0x1234, 0x472 (obj/kernel.asm)
  3. 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?
    in bootmain()
    先读一页,把header table的首末地址读到,然后遍历table的每一条记录去load segment address

repnz: ecx不为零时就反复执行;一共128次

Ex4
看书熟悉指针 #TODO

p = &c: assigns the address of c to the variable p, and p is said to "point to" c
int x = 1, y = 2, z[10];
int *ip; // ip is a pointer to int
ip = &x; // ip now points to x
y = *ip; // y is now 1
*ip = 0; // x is now 0
ip = &.z[O]; // ip now points to z[O]
The unary operators * and & bind more tightly than arithmetic operators

Ex5
An ELF binary starts with a fixed-length ELF header, followed by a variable-length program header listing each of the program sections to be loaded. The C definitions for these ELF headers are in inc/elf.h. The program sections we're interested in are:
.text: The program's executable instructions.
.rodata: Read-only data, such as ASCII string constants produced by the C compiler. (We will not bother setting up the hardware to prohibit writing, however.)
.data: The data section holds the program's initialized data, such as global variables declared with initializers like int x = 5;.
There is one more field in the ELF header that is important to us, named e_entry. This field holds the link address of the entry point in the program: the memory address in the program's text section at which the program should begin executing.
x/Nx ADDR prints N words of memory at ADDR.
0x7c00 检查全是0

0x10000c
Screen Shot 2019-03-10 at 1.34.34 AM.png

image.png

应该是kernel的text segment

Ex6
-Ttext 是0x7c00
kernel.ld里面load位置是100000
把-Ttext随便改成什么别的就行

Ex7
boot loader 的链接地址和加载地址是一样的,但是 kernel 的链接地址和加载地址有些差异。 kern/kernel.ld 可以发现内核地址链接在 0xF0100000;加载在)0x10000c(objdump)
Up until kern/entry.S sets the CR0_PG flag, memory references are treated as physical addresses
entry.S 里面置Cr0的代码是movl %eax, %cr0


执行完这句话,两个被映射到相同代码(原本存放在0xf0100000处的内容,已经被映射到0x00100000处了)
如果把这句话注释掉:movl $0x0,%ebp会挂
image.png

全局标识符表:https://wiki.osdev.org/Global_descriptor_table #TODO

Ex8


image.png

Ex9
在case d和case +里面改

  1. Explain the interface between printf.c and console.c. Specifically, what function does console.c export? How is this function used by printf.c?
    cputchar
    kern/printf.c 和 lib/printfmt.c 依赖 kern/console.c
    printf是formatting console更底层
  2. Explain the following from console.c
    如果要显示的内容过多,超过一屏大小,那么就把行从下往上移一行,然后最下面一行为空
  3. For the following questions you might wish to consult the notes for Lecture 2. These notes cover GCC's calling convention on the x86.
    Trace the execution of the following code step-by-step:
    3.1. In the call to cprintf(), to what does fmt point? To what does ap point?
    fmt: the formatting string
    ap: the pointer of the first element in the list, namely x.
    3.2. List (in order of execution) each call to cons_putc, va_arg, and vcprintf. For cons_putc, list its argument as well. For va_arg, list what ap points to before and after the call. For vcprintf list the values of its two arguments.

TODO

int cprintf("x %d, y %x, z %d\n", x, y, z);
vc_printf:fmt points to "x %d, y %x, z %d\n" and ap points to the pointer of x
vcprintf calls void vprintfmt(void (putch)(int, void), void *putdat, const char *fmt, va_list ap); putch points to the function putch; putdat equals 0; fmt and ap is the same as above.
va_arg在putch里面调用
4.Run the following code.
unsigned int i = 0x00646c72;
cprintf("H%x Wo%s", 57616, &i);
He110 World
由于x86是小端模式,代表字的最高位字节存放在最高位字节地址上。假设i变量的地址为0x00,那么i的4个字节的值存放在0x00,0x01,0x02,0x03四处。由于是小端存储,所以0x00处存放0x72('r'),0x01处存放0x6c('l'),0x025. 处存放0x64('d'),0x03处存放0x00('\0').

  1. In the following code, what is going to be printed after y=? (note: the answer is not a specific value.) Why does this happen?
    -267317640
  2. Let's say that GCC changed its calling convention so that it pushed arguments on the stack in declaration order, so that the last argument is pushed last. How would you have to change cprintf or its interface so that it would still be possible to pass it a variable number of arguments?
    reverse ap
    Ex10
    用printfmt输出错误信息
    p=putdat 然后强制转换下
    Ex11


    image.png

    注意个坑:%d里面加号要添加判断,不能直接else

Ex12
movl $(bootstacktop),%esp
%esp也就是bootstacktop的值为0xf0110000。其中 kern/entry.S 的 KSTKSIZE 应该就是堆栈的大小。栈高地址为bootstacktop的值,也就是0xf0110000。
在entry里面搜PG

Ex13
连续的函数调用,传入参数5 4 3 2 1 0依次调用
前三行的push
Ex14
题目里说:You can do it entirely in C, but you may find the read_ebp() function in inc/x86.h useful. 直接拿ebp
ebp=0停止循环
The listed eip value is the function's return instruction pointer: the instruction address to which control will return when the function returns. The return instruction pointer typically points to the instruction after the call instruction
ebp是基址指针,eip是返回指令指针
格式化输出 %08x
Ex15
读kernel.ld:什么都没看出
给的命令:都无法运行
stab binsearch有个读函数的可以依样画葫芦
注释里面有写lline<=rline代表可以找到,否则-1
Ex16
原来还可以直接用函数名作地址,以前都是先放在asm里面搜到再进gdb打断点的。。
start_overflow() ret addr替换为do_overflow()的地址
为了要保证正常退出,需要把ret addr+4的地方填上本来应该返回的地址,以便从do_overflow()返回原本应该的overflow_me(),相当于mask掉做的坏事�
两位两位处理
有个字符串str 256老发报警,改成255就好了
Ex17 搞不定command怎么处理
Ref
https://xinqiu.me/2016/10/15/MIT-6.828-1/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,033评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,725评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,473评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,846评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,848评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,691评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,053评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,700评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,856评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,676评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,787评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,430评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,034评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,990评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,218评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,174评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,526评论 2 343

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,289评论 0 10
  • 有句话说没压力就没有动力,这话还真不假,最近写文一直都是断断续续的,写的好坏是其次,但就是写不下去,有时感觉有千言...
    遇见子美一一阅读 314评论 0 4
  • 一句怀念,有点青春,又或许带点苦涩的感觉… 半个多月没回家了,比起前两年,这段时间不算长,但想家了,这两天老想睡觉...
    青梦几回眸阅读 164评论 0 1
  • 1 透明和女朋友分手是在一年前,那时也是这么炎热的天气,热得我连灌两瓶冰镇汽水才能坐下来跟透明说话。 他第一句就是...
    乾兑兑阅读 537评论 0 0