北京大学操作系统实习JOS Lab3实验报告
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SS,
Interruption gates:
IDT interruption gate IF int
int
trap gate
Call gates:
GDT
LGT
trap gate
Basics of Protected Control Transfer
Interrupt Descriptor Table (IDT): kernel
Lab3
1500012703
Introduction
Lab call exception Lab environment environment JOS process environment UNIX process kernel kernel Lab system
Part A: User Environments and Exception Handling
TF
IF
TF IF
1
gate CPL <= DPL and RPL <= DPL Despite the fact
that all gates are similar, they have some differences, because originally Intel engineers thought that different gates will be used for different purposes. Task gates: 3 IDT GDT int gate
static inline void lcr3(uint32_t val) { asm volatile("movl %0,%%cr3" : : "r" (val)); }
Env page page env
env ref kernel
len va env PGSIZE user
load_icode() boot\main.c
Creating and Running Environments
Lab kernel eip user user iret ELF entry env load_icode env_pop_tf kernel .o(ELF)
Exercise 2.
Exercise env_init() pages env_free_list env_setup_vm() kern_pgdir kernel region_alloc() e->env_pgdir page_insert PTE_U PTE_W 1 env bootmain env memset , memcpy env lcr3(PADDR(e->env_pgdir))
kernel
+--------------------+ KSTACKTOP | 0x00000 | old SS | " - 4 | old ESP | " - 8 | old EFLAGS | " - 12 | 0x00000 | old CS | " - 16 | old EIP | " - 20 <---- ESP +--------------------+
kern\trapentry.S .data .data kernel\trap.c
error code
kernel
0
error code
Nested Exceptions and Interrupts
(CSHale Waihona Puke Baidu
2
0)
CPU kernel
Nested Exceptions and Interrupts
SS ESP
error code
+--------------------+ <---- old ESP | old EFLAGS | " - 4 | 0x00000 | old CS | " - 8 | old EIP | " - 12 +--------------------+
Kernel Lab kern/env.c env_free_list Env page_free_list pages NENV Env Lab4 envs , curenv , Lab
Environment State
Env inc/env.h
struct Env { struct Trapframe env_tf; // Saved registers struct Env *env_link; // Next free Env envid_t env_id; // Unique environment identifier envid_t env_parent_id; // env_id of this env's parent enum EnvType env_type; // Indicates special system environments unsigned env_status; // Status of the environment uint32_t env_runs; // Number of times environment has run // Address space pde_t *env_pgdir; };
Intel TSS (Task State Segment)
TSS: TSS
EIP
CS x86
kernel ESP, EFLAGS, CS, EIP 0 TSS ESP0 TSS call Trap gates: gate jmp task task gate IDT TSS int task gate CS EIP SS0 kernel task gate TSS GDT ESP SS
x86
256 CPU
0
255 IDT
EIP
CS
0-1 gate
JOS
0
Task State Segment (TSS):
Types of Exceptions and Interrupts
0 31 32~255 int
Lab
0-31
0x30
Lab4
JOS
An Example
TSS ESP0 JOS GD_KD (kernel data ) KSTACKTOP kernel SS0
error code
EIP
error code
Setting Up the IDT
IDT
IDT trapentry.S trap.c
+----------------+ | &handler1 |---------> handler1: | | // do stuff | | call trap t | | // ... +----------------+ | &handler2 |--------> handler2: | | // do stuff | | call trap | | // ... +----------------+ . . . +----------------+ | &handlerX |--------> handlerX: | | // do stuff | | call trap | | // ... +----------------+
Handling Interrupts and Exceptions Exercise 3.
Chapter IDT TSS Task gates Interrupt gates Trap gates gate
gate
64
call gate Stack Overflow
eflags debug (maskable interrupt requests) gate DPL CPL RPL gate 1
// Kernel virtual address of page dir
env_tf :
Trapframe
inc/trap.h
struct Trapframe { struct PushRegs tf_regs; uint16_t tf_es; uint16_t tf_padding1; uint16_t tf_ds; uint16_t tf_padding2; uint32_t tf_trapno; /* below here defined by x86 hardware */ uint32_t tf_err; uintptr_t tf_eip; uint16_t tf_cs; uint16_t tf_padding3; uint32_t tf_eflags; /* below here only when crossing rings, such as from user to kernel */ uintptr_t tf_esp; uint16_t tf_ss; uint16_t tf_padding4; } __attribute__((packed));
0
IDT
0
entry
CS:EIP
error code error code
+--------------------+ KSTACKTOP | 0x00000 | old SS | " - 4 | old ESP | " - 8 | old EFLAGS | " - 12 | 0x00000 | old CS | " - 16 | old EIP | " - 20 | error code | " - 24 <---- ESP +--------------------+
env_pgdir : Unix JOS thread address space thread xv6 kernel env_tf JOS
address space
env_pgdir
Allocating the Environments Array Exercise 1.
Exercise UENVS envs Lab2
trap (struct Trapframe *tf) { // handle the exception/interrup }
IDT Gatedesc
Gatedesc
idt[256] Gate
inc\mmu.h
// Gate descriptors for interrupts and traps struct Gatedesc { unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment unsigned gd_sel : 16; // segment selector unsigned gd_args : 5; // # args, 0 for interrupt/trap gates unsigned gd_rsv1 : 3; // reserved(should be zero I guess) unsigned gd_type : 4; // type(STS_{TG,IG32,TG32}) unsigned gd_s : 1; // must be 0 (system) unsigned gd_dpl : 2; // descriptor(meaning new) privilege level unsigned gd_p : 1; // Present unsigned gd_off_31_16 : 16; // high bits of offset in segment };
Trapframe trapno env_link : env_id : id Env trap
es , ds
env_parent_id : env_type :
id' ENV_TYPE_USER
env_status : 1. 2. 3. 4. 5. ENV_FREE : ENV_RUNNABLE : ENV_RUNNING : ENV_NOT_RUNNABLE : ENV_DYING :
ELF
kern_pgdir env_pgdir
val lcr3
cr3
cr3 env_pgdir
env_create() env ELF e 2., 3., ... If
env_alloc
load_icode
env_run() If 1.
triple fault handout kern\init.c env_pop_tf user entry iret env_pop_tf kernel user eip ENV_CREATE .o kernel ELF