@ -0,0 +1,7 @@ | |||
#include <stdio.h> | |||
int | |||
main(void) | |||
{ | |||
printf("Hello, world!\n"); | |||
return 0; | |||
} |
@ -0,0 +1,6 @@ | |||
#README | |||
Try below command | |||
``` | |||
$gcc -g -m32 lab0_ex2.c | |||
``` | |||
Then you will get a.out. Try to use gdb to debug lab0_ex2. |
@ -0,0 +1,51 @@ | |||
#include <stdio.h> | |||
#define STS_IG32 0xE // 32-bit Interrupt Gate | |||
#define STS_TG32 0xF // 32-bit Trap Gate | |||
typedef unsigned uint32_t; | |||
#define SETGATE(gate, istrap, sel, off, dpl) { \ | |||
(gate).gd_off_15_0 = (uint32_t)(off) & 0xffff; \ | |||
(gate).gd_ss = (sel); \ | |||
(gate).gd_args = 0; \ | |||
(gate).gd_rsv1 = 0; \ | |||
(gate).gd_type = (istrap) ? STS_TG32 : STS_IG32; \ | |||
(gate).gd_s = 0; \ | |||
(gate).gd_dpl = (dpl); \ | |||
(gate).gd_p = 1; \ | |||
(gate).gd_off_31_16 = (uint32_t)(off) >> 16; \ | |||
} | |||
/* Gate descriptors for interrupts and traps */ | |||
struct gatedesc { | |||
unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment | |||
unsigned gd_ss : 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 | |||
}; | |||
int | |||
main(void) | |||
{ | |||
unsigned before; | |||
unsigned intr; | |||
unsigned after; | |||
struct gatedesc gintr; | |||
intr=8; | |||
before=after=0; | |||
gintr=*((struct gatedesc *)&intr); | |||
SETGATE(gintr, 0,1,2,3); | |||
intr=*(unsigned *)&(gintr); | |||
printf("intr is 0x%x\n",intr); | |||
printf("gintr is 0x%llx\n",gintr); | |||
return 0; | |||
} |
@ -0,0 +1,53 @@ | |||
#README | |||
Try below command | |||
``` | |||
gcc -g -m32 lab0_ex3.c 2>&1|tee make.log | |||
``` | |||
If you get gcc's error, try to read make.log and fix the bugs. | |||
If gcc successed, then you will get a.out. | |||
Try to answer below question. | |||
对于如下的代码段, | |||
``` | |||
... | |||
#define STS_CG32 0xC // 32-bit Call Gate | |||
#define STS_IG32 0xE // 32-bit Interrupt Gate | |||
#define SETGATE(gate, istrap, sel, off, dpl) { \ | |||
(gate).gd_off_15_0 = (uint32_t)(off) & 0xffff; \ | |||
(gate).gd_ss = (sel); \ | |||
(gate).gd_args = 0; \ | |||
(gate).gd_rsv1 = 0; \ | |||
(gate).gd_type = (istrap) ? STS_TG32 : STS_IG32; \ | |||
(gate).gd_s = 0; \ | |||
(gate).gd_dpl = (dpl); \ | |||
(gate).gd_p = 1; \ | |||
(gate).gd_off_31_16 = (uint32_t)(off) >> 16; \ | |||
} | |||
/* Gate descriptors for interrupts and traps */ | |||
struct gatedesc { | |||
unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment | |||
unsigned gd_ss : 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 | |||
}; | |||
... | |||
``` | |||
如果在其他代码段中有如下语句, | |||
``` | |||
unsigned intr; | |||
intr=8; | |||
SETGATE(intr, 0,1,2,3); | |||
``` | |||
请问执行上述指令后, intr的值是多少? | |||