主頁 > 作業系統 > MIT6.828——Lab1 partB(麻省理工作業系統課程實驗)

MIT6.828——Lab1 partB(麻省理工作業系統課程實驗)

2021-10-11 07:22:16 作業系統

Lab1

歷時2天,完成了LAB1,完整代碼倉庫可點擊:https://github.com/Elio-yang/MIT6.828

partA 練習

  • exercise3

gdb指令:

x/Ni addr :反匯編addr處的N條指令

x/Nx addr:列印N位元組addr處的記憶體

b *addr:在addr處設定斷點

readsect(): 0x7c7c

bootmain():0x7d25

回圈結束的第一條指令是0x7d81處的call *0x10018,利用gdb0x10018記憶體處的值為0x10000c,故第一條指令是call 0x10000c,這個地址就是kernel的entry,

At what point does the processor start executing 32-bit code? What exactly causes the switch from 16- to 32-bit mode?

ljmp $PROT_MODE_CSEG,$protcseg這條指令后開始執行32位代碼,真正造成切換的,是CR0PE位被置為,進入了保護模式,

What is the last instruction of the boot loader executed, and what is the first instruction of the kernel it just loaded?

last:

 call *0x10018

first:

f010000c <entry>:
f010000c:	66 c7 05 72 04 00 00 	movw   $0x1234,0x472

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?

都是通過ELF header得知的,

Loading the kernel

第一個需要注意的是代碼的鏈接地址和裝載地址

使用命令

objdump -h <file.o>
# -x Display all available header information 
# -f Display entry point
# 更多用法 man objdump 即可

在kernel中這兩者是不同的,但是在之前的boot中

二者是一致的,在kern/entry.S中有這樣一段代碼

# Turn on paging.
movl	%cr0, %eax
orl	$(CR0_PE|CR0_PG|CR0_WP), %eax
movl	%eax, %cr0

這便開啟了地址映射,在此之前kernel的VMA和LMA地址處的記憶體一般是不同的,但是開啟分頁之后,LMA映射到了VMA,

The Kernel

第一個值得注意的是:開啟分頁模式,將虛擬地址[0, 4MB)映射到物理地址[0, 4MB),[0xF0000000, 0xF0000000+4MB)映射到[0, 4MB)(/kern/entry.S)

分頁模式下的尋址,在Intel手冊中也有給出

開啟這個模式的代碼如下

# Load the physical address of entry_pgdir into cr3.  entry_pgdir
# is defined in entrypgdir.c.
movl	$(RELOC(entry_pgdir)), %eax
movl	%eax, %cr3
# Turn on paging.
movl	%cr0, %eax
orl	$(CR0_PE|CR0_PG|CR0_WP), %eax
movl	%eax, %cr0

關于地址的映射在kern/entrypgdir.c有代碼實作

__attribute__((__aligned__(PGSIZE)))
pde_t entry_pgdir[NPDENTRIES] = {
	// Map VA's [0, 4MB) to PA's [0, 4MB)
	[0]
		= ((uintptr_t)entry_pgtable - KERNBASE) + PTE_P,
	// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
	[KERNBASE>>PDXSHIFT]
		= ((uintptr_t)entry_pgtable - KERNBASE) + PTE_P + PTE_W
};

編譯器分配的空間是強制性4kB頁對齊的,pgdir是一個1024項的陣列,這里可以不用詳細了解原理For now, you don't have to understand the details of how this works, just the effect that it accomplishes.

  • exercise7

在開啟分頁之前,這兩個地址內容是不一致的,后面經過了地址映射,這兩者的內容一致,注釋掉

movl %eax, %cr0程式會崩潰,

Formated Printing to the Console

首先是幾個函式的呼叫關系

然后練習題

  • exercise8

這個檔案就是lib/printfmt.c

// (unsigned) octal
case 'o':
    // Replace this with your code.
    num=getuint(&ap,lflag);
    base=8;
    goto number;

對照背景關系很容易補全,

下面是回答問題:

Explain the interface between printf.c and console.c. Specifically, what function does console.c export? How is this function used by printf.c?

對照上文呼叫關系圖即可

Explain the following from console.c

if (crt_pos >= CRT_SIZE) {
    int i;
    memmove(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) *sizeof(uint16_t));
    for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i++)
        crt_buf[i] = 0x0700 | ' ';
    crt_pos -= CRT_COLS;
}

首先文本模式最多能顯示25*80個字符,即25行每行80個,此處

// console.h
#define CRT_ROWS	25
#define CRT_COLS	80
#define CRT_SIZE	(CRT_ROWS * CRT_COLS)

因此,這一段處理的是超出一螢屏以后的做法:即舍棄最上面一行,整體上移一行,

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:

int x = 1, y = 3, z = 4;
cprintf("x %d, y %x, z %d\n", x, y, z);
  • In the call to cprintf(), to what does fmt point? To what does ap point?
  • 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.

GCC 函式呼叫約定是引數從右往左入堆疊,此處fmt指向的就是第一個引數的位置,而ap指向第一個可變引數,也就是第二個引數x的位置,關于變引數,JOS使用的是GCC builtin來實作的,其實作可以用如下代碼進行大致說明(不是嚴謹的完整實作):

#define va_start(list,param_1st)   ( list = (va_list)&param1+ sizeof(param_1st) )
#define va_arg(list,type)   ( (type *) ( list += sizeof(type) ) )[-1]
#define va_end(list) ( list = (va_list)0 )

因此:

va_list:即char*

va_start:獲取第一個可變引數的地址

va_arg:回傳指向下一個引數的指標

va_end:清空引數串列

Run the following code.

    unsigned int i = 0x00646c72;
    cprintf("H%x Wo%s", 57616, &i);

What is the output? Explain how this output is arrived at in the step-by-step manner of the previous exercise.

Here's an ASCII tablethat maps bytes to characters.

The output depends on that fact that the x86 is little-endian. If the x86 were instead big-endian what would you set i to in order to yield the same output? Would you need to change 57616 to a different value?

Here's a description of little- and big-endian and a more whimsical description.

把這段代碼加入init.c中,運行make qemu,結果如下

0xe110=57616這很好解釋,查閱ASCII表,得知

00(\0) 64(d) 6c(l) 72(r)

顯然這是由于小端模式而使用的一個數,為了證明這一點,可以輸出&i記憶體處的位元組,將下面這段代碼放在上面列印代碼的后面

cprintf("addr of i: %p\n",&i);
char *p=(char*)&i;
for(int i=0;i<4;i++){
	cprintf("[%x]",*p);
	p++;
}

輸出結果如下:

In the following code, what is going to be printed after'y='? (note: the answer is not a specific value.) Why does this happen?

 cprintf("x=%d y=%d", 3);

運行結果如下

顯然y的值并不一定固定,他就是把記憶體中那個位置的數拿來充當了第二個引數,

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?

更改了入堆疊方式,相應地更改va_startva_start即可,

The Stack

先看這個練習

  • exercise9

entry.S中可以找到如下代碼

# where the stack is set.
# Clear the frame pointer register (EBP)
# so that once we get into debugging C code,
# stack backtraces will be terminated properly.
movl	$0x0,%ebp			# nuke frame pointer
# Set the stack pointer
movl	$(bootstacktop),%esp
# now to C code
call	i386_init

利用gdb得知,movl $(bootstacktop),%esp會被編譯為movl $0xf0110000,%esp,因此堆疊何時初始化,堆疊放在哪兒都清楚了,繼續看代碼

###################################################################
# boot stack
###################################################################
	.p2align	PGSHIFT		# force page alignment
	.globl		bootstack
bootstack:
	.space		KSTKSIZE
	.globl		bootstacktop   

這便開辟了堆疊的大小,即32KB,堆疊由高地址向低地址增長,

下面,關于函式的呼叫程序,做一個總結,可以參考[CSAPP,p164],

這是從課件ppt截取的兩頁

關于函式的呼叫,一般有如下的動作發生:

  1. 函式呼叫者(caller)將引數入堆疊,按照從右到左的順序入堆疊
  2. call指令會自動將當前%eip(指向call的后面一條指令)入堆疊,ret指令將自動從堆疊中彈出該值到eip暫存器
  3. 被呼叫函式(callee)負責:將%ebp入堆疊,%esp的值賦給%ebp,

因此函式開頭都會是類似的兩條指令

push %ebp
mov %esp,%ebp

因此整個呼叫鏈差不多可以描述成如下形式

來到下一個練習

  • exercise10

每次call之后會干什么,上文已經分析了,至于每次遞回入堆疊的字,偽代碼可以表示為

push %eip
push %ebp
push %esi
push %ebx

共計0x10B,

  • exercise11

需要我們更改mom_backtrace()函式,達到的效果如下:

題目中已經說明,獲得%ebp的函式就是read_ebp(),那么編碼作業應該很好完成了(利用呼叫鏈中%ebp的鏈)

int
mon_backtrace(int argc, char **argv, struct Trapframe *tf)
{
	// Your code here.
	uint32_t *ebp=(uint32_t*)read_ebp();
	while(ebp!=NULL){
	    cprintf("ebp %8x  eip %8x  args %08x %08x %08x %08x %08x\n",
			ebp,ebp[1],ebp[2],ebp[3],ebp[4],ebp[5],ebp[6]);
	    ebp=(uint32_t *)(*ebp);
	}
	return 0;
}

運行結果如下

  • exercise12

練習12的任務有三個:

  1. 搞清楚__STAB_*
  2. 添加命令backtrace
  3. 完善mon_backtrace
  • 任務一

    根據提示,查看這幾個檔案,首先是kernel.ld

    .stab : {
                    PROVIDE(__STAB_BEGIN__ = .);
                    *(.stab);
                    PROVIDE(__STAB_END__ = .);
                    BYTE(0)         /* Force the linker to allocate space
                                       for this section */
            }
     
    .stabstr : {
                    PROVIDE(__STABSTR_BEGIN__ = .);
                    *(.stabstr);
                    PROVIDE(__STABSTR_END__ = .);
                    BYTE(0)         /* Force the linker to allocate space
                                       for this section */
            }
    

    可以知道.stab.stabstr應該是兩個段,

    接著 objdump -h obj/kern/kernel

然后是``objdump -G obj/kern/kernel``

執行后面的操作以后,大致可以知道這是一個段,包含了除錯資訊(符號表),細節可以不用太了解,接著找到``stab.h``,其中

這兩項便是后文編碼尋找行號時需要的,下面開始任務二和三
  • 任務二

    題目中提示了需要使用debuginfo_eip,查找這個函式發現,他會將需要的資訊存到型別為struct Eipdebuginfo的結構體中,查看該結構體定義(kern/kdebebug.h)

    // Debug information about a particular instruction pointer
    struct Eipdebuginfo {
    	const char *eip_file;		// Source code filename for EIP
    	int eip_line;			    // Source code linenumber for EIP
    
    	const char *eip_fn_name;	// Name of function containing EIP
    					            //  - Note: not null terminated!
        
    	int eip_fn_namelen;		// Length of function name
    	uintptr_t eip_fn_addr;		// Address of start of function
    	int eip_fn_narg;		// Number of function arguments
    };
    

    因此只需要使用debuginfo_eip填充該結構體,再輸出資訊即可,

    static struct Command commands[] = {
    	{ "help", "Display this list of commands", mon_help },
    	{ "kerninfo", "Display information about the kernel", mon_kerninfo },
    	{ "backtrace", "Show stack backtrace",mon_stacktrace}	
    };
    //......
    int 
    for_stack(int argc,char **argv,struct Trapframe *tf)
    {
    	uint32_t *ebp=(uint32_t*)read_ebp();
    	while(ebp!=NULL){
    	    struct Eipdebuginfo info;
    	    uint32_t eip = ebp[1];
    	    debuginfo_eip((int)eip, &info);
    	    cprintf("  ebp %8x  eip %8x  args %08x %08x %08x %08x %08x\n",
    			ebp,ebp[1],ebp[2],ebp[3],ebp[4],ebp[5],ebp[6]);
    	    const  char* filename=(&info)->eip_file;
    	    int line = (&info)->eip_line;
    	    const char * not_null_ter_fname=(&info)->eip_fn_name;
    	    int offset = (int)(eip)-(int)((&info)->eip_fn_addr);
    	    cprintf("        %s:%d:  %.*s+%d\n",filename,line,info.eip_fn_namelen,not_null_ter_fname,offset);
    	    ebp=(uint32_t *)(*ebp);
    	}
    	return 0;
    }
    int
    mon_stacktrace(int argc,char **argv,struct Trapframe *tf)
    {
        cprintf("Stack backtrace:\n");
        return for_stack(argc,argv,tf);
    }
    

    其中關于檔案行號的查找實作,對照背景關系就能實作,注意N_SLINE這就是之前說stab時提到的一個有用的屬性,

    // Search within [lline, rline] for the line number stab.
    // If found, set info->eip_line to the right line number.
    // If not found, return -1.
    //
    // Hint:
    //	There's a particular stabs type used for line numbers.
    //	Look at the STABS documentation and <inc/stab.h> to find
    //	which one.
    // Your code here.
    stab_binsearch(stabs, &lline, &rline, N_SLINE, addr);
    if(lline<=rline){
        info->eip_line=stabs[lline].n_desc;
    }else{
        return -1;
    }
    

    運行結果如下:

之后運行評分程式

至此,Lab1完結,完整代碼倉庫可點擊:https://github.com/Elio-yang/MIT6.828

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/308090.html

標籤:其他

上一篇:bochs(2.6.11)配置安裝

下一篇:bash是什么?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • CA和證書

    1、在 CentOS7 中使用 gpg 創建 RSA 非對稱密鑰對 gpg --gen-key #Centos上生成公鑰/密鑰對(存放在家目錄.gnupg/) 2、將 CentOS7 匯出的公鑰,拷貝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公鑰加密一個檔案 gpg -a ......

    uj5u.com 2020-09-10 00:09:53 more
  • Kubernetes K8S之資源控制器Job和CronJob詳解

    Kubernetes的資源控制器Job和CronJob詳解與示例 ......

    uj5u.com 2020-09-10 00:10:45 more
  • VMware下安裝CentOS

    VMware下安裝CentOS 一、軟硬體準備 1 Centos鏡像準備 1.1 CentOS鏡像下載地址 下載地址 1.2 CentOS鏡像下載程序 點擊下載地址進入如下圖的網站,選擇需要下載的版本,這里選擇的是Centos8,點擊如圖所示。 決定選擇Centos8后,選擇想要的鏡像源進行下載,此 ......

    uj5u.com 2020-09-10 00:12:10 more
  • 如何使用Grep命令查找多個字串

    如何使用Grep 命令查找多個字串 大家好,我是良許! 今天向大家介紹一個非常有用的技巧,那就是使用 grep 命令查找多個字串。 簡單介紹一下,grep 命令可以理解為是一個功能強大的命令列工具,可以用它在一個或多個輸入檔案中搜索與正則運算式相匹配的文本,然后再將每個匹配的文本用標準輸出的格式 ......

    uj5u.com 2020-09-10 00:12:28 more
  • git配置http代理

    git配置http代理 經常遇到克隆 github 慢的問題,這里記錄一下幾種配置 git 代理的方法,解決 clone github 過慢。 目錄 git配置代理 git單獨配置github代理 git配置全域代理 配置終端環境變數 git配置代理 主要使用 git config 命令 git單獨 ......

    uj5u.com 2020-09-10 00:12:33 more
  • Linux npm install 裝包時提示Error EACCES permission denied解

    npm install 裝包時提示Error EACCES permission denied解決辦法 ......

    uj5u.com 2020-09-10 00:12:53 more
  • Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包

    Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包。 18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx 19 已加載插件:fastestmirror, langpacks 20 Loading ......

    uj5u.com 2020-09-10 00:13:13 more
  • Linux查看服務器暴力破解ssh IP

    在公網的服務器上經常遇到別人爆破你服務器的22埠,用來挖礦或者干其他嘿嘿嘿的事情~ 這種情況下正確的做法是: 修改默認ssh的22埠 使用設定密鑰登錄或者白名單ip登錄 建議服務器密碼為復雜密碼 創建普通用戶登錄服務器(root權限過大) 建立堡壘機,實作統一管理服務器 統計爆破IP [root ......

    uj5u.com 2020-09-10 00:13:17 more
  • CentOS 7系統常見快捷鍵操作方式

    Linux系統中一些常見的快捷方式,可有效提高操作效率,在某些時刻也能避免操作失誤帶來的問題。 ......

    uj5u.com 2020-09-10 00:13:31 more
  • CentOS 7作業系統目錄結構介紹

    作業系統存在著大量的資料檔案資訊,相應檔案資訊會存在于系統相應目錄中,為了更好的管理資料資訊,會將系統進行一些目錄規劃,不同目錄存放不同的資源。 ......

    uj5u.com 2020-09-10 00:13:35 more
最新发布
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:43:21 more
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:42:36 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:26:53 more
  • 設定Windows主機的瀏覽器為wls2的默認瀏覽器

    這里以Chrome為例。 1. 準備作業 wsl是可以使用Windows主機上安裝的exe程式,出于安全考慮,默認情況下改功能是無法使用。要使用的話,終端需要以管理員權限啟動。 我這里以Windows Terminal為例,介紹如何默認使用管理員權限打開終端,具體操作如下圖所示: 2. 操作 wsl ......

    uj5u.com 2023-04-19 09:25:49 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:19:04 more
  • Linux學習筆記

    IP地址和主機名 IP地址 ifconfig可以用來查詢本機的IP地址,如果不能使用,可以通過install net-tools安裝。 Centos系統下ens33表示主網卡;inet后表示IP地址;lo表示本地回環網卡; 127.0.0.1表示代指本機;0.0.0.0可以用于代指本機,同時在放行設 ......

    uj5u.com 2023-04-18 06:52:01 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:50 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:01 more
  • 你是不是暴露了?

    作者:袁首京 原創文章,轉載時請保留此宣告,并給出原文連接。 如果您是計算機相關從業人員,那么應該經歷不止一次網路安全專項檢查了,你肯定是收到過資訊系統技術檢測報告,要求你加強風險監測,確保你提供的系統服務堅實可靠了。 沒檢測到問題還好,檢測到問題的話,有些處理起來還是挺麻煩的,尤其是線上正在運行的 ......

    uj5u.com 2023-04-05 16:52:56 more
  • 細節拉滿,80 張圖帶你一步一步推演 slab 記憶體池的設計與實作

    1. 前文回顧 在之前的幾篇記憶體管理系列文章中,筆者帶大家從宏觀角度完整地梳理了一遍 Linux 記憶體分配的整個鏈路,本文的主題依然是記憶體分配,這一次我們會從微觀的角度來探秘一下 Linux 內核中用于零散小記憶體塊分配的記憶體池 —— slab 分配器。 在本小節中,筆者還是按照以往的風格先帶大家簡單 ......

    uj5u.com 2023-04-05 16:44:11 more