diff --git a/15_day/bootpack.c b/15_day/bootpack.c index 4b3e280..98b7bc4 100644 --- a/15_day/bootpack.c +++ b/15_day/bootpack.c @@ -7,6 +7,15 @@ void make_window8(unsigned char *buf, int xsize, int ysize, char *title); void putfonts8_asc_sht(struct SHEET *sht, int x, int y, int c, int b, char *s, int l); void make_textbox8(struct SHEET *sht, int x0, int y0, int sx, int sy, int c); +struct TSS32 { + int backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3; + int eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi; + int es, cs, ss, ds, fs, gs; + int ldtr, iomap; +}; + +void task_b_main(void); + void HariMain(void) { struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; @@ -14,7 +23,7 @@ void HariMain(void) char s[40]; int fifobuf[128]; struct TIMER *timer, *timer2, *timer3; - int mx, my, i, cursor_x, cursor_c; + int mx, my, i, cursor_x, cursor_c, task_b_esp; unsigned int memtotal; struct MOUSE_DEC mdec; struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR; @@ -29,6 +38,8 @@ void HariMain(void) 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.' }; + struct TSS32 tss_a, tss_b; + struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT; init_gdtidt(); init_pic(); @@ -85,6 +96,31 @@ void HariMain(void) memtotal / (1024 * 1024), memman_total(memman) / 1024); putfonts8_asc_sht(sht_back, 0, 32, COL8_FFFFFF, COL8_008484, s, 40); + tss_a.ldtr = 0; + tss_a.iomap = 0x40000000; + tss_b.ldtr = 0; + tss_b.iomap = 0x40000000; + set_segmdesc(gdt + 3, 103, (int) &tss_a, AR_TSS32); + set_segmdesc(gdt + 4, 103, (int) &tss_b, AR_TSS32); + load_tr(3 * 8); + task_b_esp = memman_alloc_4k(memman, 64 * 1024) + 64 * 1024; + tss_b.eip = (int) &task_b_main; + tss_b.eflags = 0x00000202; /* IF = 1; */ + tss_b.eax = 0; + tss_b.ecx = 0; + tss_b.edx = 0; + tss_b.ebx = 0; + tss_b.esp = task_b_esp; + tss_b.ebp = 0; + tss_b.esi = 0; + tss_b.edi = 0; + tss_b.es = 1 * 8; + tss_b.cs = 2 * 8; + tss_b.ss = 1 * 8; + tss_b.ds = 1 * 8; + tss_b.fs = 1 * 8; + tss_b.gs = 1 * 8; + for (;;) { io_cli(); if (fifo32_status(&fifo) == 0) { @@ -149,6 +185,7 @@ void HariMain(void) } } else if (i == 10) { /* 10秒定时器 */ putfonts8_asc_sht(sht_back, 0, 64, COL8_FFFFFF, COL8_008484, "10[sec]", 7); + taskswitch4(); } else if (i == 3) { /* 3秒定时器 */ putfonts8_asc_sht(sht_back, 0, 80, COL8_FFFFFF, COL8_008484, "3[sec]", 6); } else if (i <= 1) { /* 光标用定时器*/ @@ -238,3 +275,8 @@ void make_textbox8(struct SHEET *sht, int x0, int y0, int sx, int sy, int c) boxfill8(sht->buf, sht->bxsize, c, x0 - 1, y0 - 1, x1 + 0, y1 + 0); return; } + +void task_b_main(void) +{ + for (;;) { io_hlt(); } +} diff --git a/15_day/bootpack.h b/15_day/bootpack.h index 2db4a08..7028583 100644 --- a/15_day/bootpack.h +++ b/15_day/bootpack.h @@ -22,11 +22,13 @@ void load_gdtr(int limit, int addr); void load_idtr(int limit, int addr); int load_cr0(void); void store_cr0(int cr0); +void load_tr(int tr); void asm_inthandler20(void); void asm_inthandler21(void); void asm_inthandler27(void); void asm_inthandler2c(void); unsigned int memtest_sub(unsigned int start, unsigned int end); +void taskswitch4(void); /* fifo.c */ struct FIFO32 { @@ -87,6 +89,7 @@ void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar); #define LIMIT_BOTPAK 0x0007ffff #define AR_DATA32_RW 0x4092 #define AR_CODE32_ER 0x409a +#define AR_TSS32 0x0089 #define AR_INTGATE32 0x008e /* int.c */ @@ -124,7 +127,6 @@ int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat); /* memory.c */ #define MEMMAN_FREES 4090 /* 大约是32KB*/ #define MEMMAN_ADDR 0x003c0000 - struct FREEINFO { /* 可用信息 */ unsigned int addr, size; }; @@ -132,7 +134,6 @@ struct MEMMAN { /* 内存管理 */ int frees, maxfrees, lostsize, losts; struct FREEINFO free[MEMMAN_FREES]; }; - unsigned int memtest(unsigned int start, unsigned int end); void memman_init(struct MEMMAN *man); unsigned int memman_total(struct MEMMAN *man); @@ -143,21 +144,17 @@ int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size); /* sheet.c */ #define MAX_SHEETS 256 -#define SHEET_USE 1 - struct SHEET { unsigned char *buf; int bxsize, bysize, vx0, vy0, col_inv, height, flags; struct SHTCTL *ctl; }; - struct SHTCTL { unsigned char *vram, *map; int xsize, ysize, top; struct SHEET *sheets[MAX_SHEETS]; struct SHEET sheets0[MAX_SHEETS]; }; - struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize); struct SHEET *sheet_alloc(struct SHTCTL *ctl); void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv); @@ -167,7 +164,7 @@ void sheet_slide(struct SHEET *sht, int vx0, int vy0); void sheet_free(struct SHEET *sht); /* timer.c */ -#define MAX_TIMER 500 +#define MAX_TIMER 500 struct TIMER { struct TIMER *next; unsigned int timeout, flags; @@ -180,11 +177,9 @@ struct TIMERCTL { struct TIMER timers0[MAX_TIMER]; }; extern struct TIMERCTL timerctl; - void init_pit(void); struct TIMER *timer_alloc(void); void timer_free(struct TIMER *timer); void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data); void timer_settime(struct TIMER *timer, unsigned int timeout); void inthandler20(int *esp); - diff --git a/15_day/naskfunc.nas b/15_day/naskfunc.nas index 995d540..0a1e37f 100644 --- a/15_day/naskfunc.nas +++ b/15_day/naskfunc.nas @@ -12,9 +12,11 @@ GLOBAL _io_load_eflags, _io_store_eflags GLOBAL _load_gdtr, _load_idtr GLOBAL _load_cr0, _store_cr0 + GLOBAL _load_tr GLOBAL _asm_inthandler20, _asm_inthandler21 GLOBAL _asm_inthandler27, _asm_inthandler2c GLOBAL _memtest_sub + GLOBAL _taskswitch4 EXTERN _inthandler20, _inthandler21 EXTERN _inthandler27, _inthandler2c @@ -104,6 +106,10 @@ _store_cr0: ; void store_cr0(int cr0); MOV CR0,EAX RET +_load_tr: ; void load_tr(int tr); + LTR [ESP+4] ; tr + RET + _asm_inthandler20: PUSH ES PUSH DS @@ -200,3 +206,7 @@ mts_fin: POP ESI POP EDI RET + +_taskswitch4: ; void taskswitch4(void); + JMP 4*8:0 + RET