From ae40a40fb1481f37af719887d3ba0de5dfc33938 Mon Sep 17 00:00:00 2001 From: catfood Date: Sat, 31 Dec 2022 22:29:46 +0800 Subject: [PATCH] fix memory bugs and clear up shits --- Makefile | 4 +- include/global.h | 2 +- include/keyboard.h | 10 -- include/proto.h | 5 +- include/tty.h | 1 - kernel/exec.c | 12 +- kernel/fork.c | 5 +- kernel/fs.c | 5 +- kernel/keyboard.c | 359 +++++---------------------------------------- kernel/main.c | 3 +- kernel/memman.c | 76 +--------- kernel/pagetbl.c | 67 +++------ kernel/start.c | 2 +- kernel/tty.c | 225 +++------------------------- kernel/vfs.c | 5 +- user/Makefrag | 3 +- user/shell_1.c | 54 +++++++ user/test2.c | 40 +++++ 18 files changed, 191 insertions(+), 687 deletions(-) create mode 100644 user/shell_1.c create mode 100644 user/test2.c diff --git a/Makefile b/Makefile index 5dfba95..8d27266 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,9 @@ gdb: $(IMAGE) @qemu-system-i386 \ -boot order=a \ -drive file=$<,format=raw \ - -s -S \ + -serial stdio \ + -s -S & + @x-terminal-emulator -e "make monitor" gdb-no-graphic: $(IMAGE) @qemu-system-i386 \ diff --git a/include/global.h b/include/global.h index 096d287..01d7751 100644 --- a/include/global.h +++ b/include/global.h @@ -36,7 +36,7 @@ extern irq_handler irq_table[]; #include "tty.h" #include "console.h" -extern TTY tty_table[]; +// extern TTY tty_table[]; extern CONSOLE console_table[]; extern int current_console; diff --git a/include/keyboard.h b/include/keyboard.h index 11ca827..3564903 100644 --- a/include/keyboard.h +++ b/include/keyboard.h @@ -158,16 +158,6 @@ typedef struct mouse_inbuf{ u8 buf[MOUSE_IN_BYTES]; }MOUSE_INPUT; -typedef struct MouseState -{ - u8 mouse_lb; - u8 mouse_mb; - u8 mouse_rb; - signed char mouse_scroll; - int mouse_x; - int mouse_y; -} MouseState; - #endif /* _ORANGES_KEYBOARD_H_ */ diff --git a/include/proto.h b/include/proto.h index daf4d29..ae50a62 100644 --- a/include/proto.h +++ b/include/proto.h @@ -47,9 +47,10 @@ void keyboard_read(); //added by mingxuan 2019-5-19 void in_process(TTY* p_tty,u32 key); void task_tty(); -void tty_write(TTY* tty, char* buf, int len); -int tty_read(TTY* tty, char* buf, int len); +void tty_write(NTTY* tty, char* buf, int len); +int tty_read(NTTY* tty, char* buf, int len); void init_tty_main(); +NTTY* get_tty(const int nr_tty); /* printf.c */ //added by mingxuan 2019-5-19 diff --git a/include/tty.h b/include/tty.h index 0a7d406..83d9029 100644 --- a/include/tty.h +++ b/include/tty.h @@ -119,6 +119,5 @@ int ps2_tty_read(NTTY* tty, char* buf, int nr); // extern int cur_ntty; extern NTTY* cur_ntty; -extern NTTY* default_ntty; // extern NTTY ntty_table[3]; #endif /* _ORANGES_TTY_H_ */ \ No newline at end of file diff --git a/kernel/exec.c b/kernel/exec.c index d519a55..62731c5 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -52,9 +52,9 @@ u32 sys_exec(char *path) // u32 fd = fake_open(path,"r"); //modified by xw, 18/5/30 /*************获取elf信息**************/ - Echo_Ehdr = sys_kmalloc(sizeof(Elf32_Ehdr)); + Echo_Ehdr = (Elf32_Ehdr*)K_PHY2LIN(sys_kmalloc(sizeof(Elf32_Ehdr))); read_Ehdr(fd, Echo_Ehdr, 0); - Echo_Phdr = sys_kmalloc(sizeof(Elf32_Phdr) * Echo_Ehdr->e_phnum); + Echo_Phdr = (Elf32_Phdr*)K_PHY2LIN(sys_kmalloc(sizeof(Elf32_Phdr) * Echo_Ehdr->e_phnum)); for (int i = 0 ; i < Echo_Ehdr->e_phnum ; i++) read_Phdr(fd, Echo_Phdr + i, Echo_Ehdr->e_phoff + i * sizeof(Elf32_Phdr)); @@ -169,15 +169,15 @@ static u32 exec_load(u32 fd,const Elf32_Ehdr* Echo_Ehdr,const Elf32_Phdr Echo_Ph {//最后一个program break; } - if( Echo_Phdr[ph_num].p_flags == 0x5) //101,R E - {//.text + if( Echo_Phdr[ph_num].p_flags == 0x5) //101,R_E + {//Code Segment exec_elfcpy(fd,Echo_Phdr[ph_num],PG_P | PG_USU | PG_RWR);//进程代码段 // kprintf("text lin base: 0x%08x\n", Echo_Phdr[ph_num].p_vaddr); p_proc_current->task.memmap.text_lin_base = Echo_Phdr[ph_num].p_vaddr; p_proc_current->task.memmap.text_lin_limit = Echo_Phdr[ph_num].p_vaddr + Echo_Phdr[ph_num].p_memsz; } - else if(Echo_Phdr[ph_num].p_flags == 0x4)//110,R - {//.data + else if(Echo_Phdr[ph_num].p_flags == 0x4)//110,R__ + {// It is virtually ROData Segment, god knows what those senpai think about this exec_elfcpy(fd,Echo_Phdr[ph_num],PG_P | PG_USU | PG_RWW);//进程数据段 // kprintf("data lin base: 0x%08x\n", Echo_Phdr[ph_num].p_vaddr); p_proc_current->task.memmap.data_lin_base = Echo_Phdr[ph_num].p_vaddr; diff --git a/kernel/fork.c b/kernel/fork.c index f7165b2..112b3f5 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -9,6 +9,7 @@ #include "proc.h" #include "global.h" #include "proto.h" +#include "stdio.h" static int fork_mem_cpy(u32 ppid,u32 pid); static int fork_pcb_cpy(PROCESS* p_child); @@ -56,9 +57,7 @@ int sys_fork() /****************用户进程数+1****************************/ u_proc_sum += 1; - disp_color_str("[fork success:",0x72); - disp_color_str(p_proc_current->task.p_name,0x72); - disp_color_str("]",0x72); + // kprintf("\x1b[32mfork success %s \x1b[0m\n", p_proc_current->task.p_name); //anything child need is prepared now, set its state to ready. added by xw, 17/12/11 p_child->task.stat = READY; diff --git a/kernel/fs.c b/kernel/fs.c index 11d341b..051e83d 100644 --- a/kernel/fs.c +++ b/kernel/fs.c @@ -1263,11 +1263,12 @@ static int do_rdwt(MESSAGE *fs_msg) if (fs_msg->type == DEV_READ) { - fs_msg->CNT = tty_read(&tty_table[nr_tty], buf, len); + fs_msg->CNT = tty_read(get_tty(nr_tty), buf, len); } else if (fs_msg->type == DEV_WRITE) { - tty_write(&tty_table[nr_tty], buf, len); + // kprintf("pid %d, tty %d; ", p_proc_current->task.pid, nr_tty); + tty_write(get_tty(nr_tty), buf, len); } return fs_msg->CNT; diff --git a/kernel/keyboard.c b/kernel/keyboard.c index 2376e10..7ecfef6 100644 --- a/kernel/keyboard.c +++ b/kernel/keyboard.c @@ -9,11 +9,11 @@ #include "proto.h" #include "keyboard.h" // #include "keymap.h" -#include "spinlock.h" #include "x86.h" #include "stdio.h" #include "assert.h" #include "minix_keymap.h" +#include "memman.h" static KB_INPUT kb_in; static MOUSE_INPUT mouse_in; @@ -29,16 +29,13 @@ static int ctrl_r; /* l ctrl state */ static int caps_lock; /* Caps Lock */ static int num_lock; /* Num Lock */ static int scroll_lock; /* Scroll Lock */ -static MouseState mouse_state; -static u8 get_byte_from_kb_buf(); static void set_leds(); static void set_mouse_leds(); static void kb_wait(); static void ps2_push(NTTY *tty, u32 key); static void kbd_process(unsigned char scode); -// static void kb_ack(); /* * Helper Procedures for PS/2 Mouse Operation * */ @@ -99,21 +96,8 @@ static u8 mouse_get_id() void kb_handler(int irq) { u8 scan_code = inb(PS2_PORT_DATA); -#ifdef DEBUGNEW kbd_process(scan_code); - // kprintf("shit"); -#else - if (kb_in.count < KB_IN_BYTES) - { - *(kb_in.p_head) = scan_code; - kb_in.p_head++; - if (kb_in.p_head == kb_in.buf + KB_IN_BYTES) - { - kb_in.p_head = kb_in.buf; - } - kb_in.count++; - } -#endif + // kprintf("kb"); }; void mouse_handler(int irq) @@ -218,251 +202,6 @@ void init_kb() init_mouse(); set_mouse_leds(); } -#ifndef DEBUGNEW -static int column; -void keyboard_read(TTY *p_tty) -{ - u8 scan_code; - - /** - * 1 : make - * 0 : break - */ - int make; - - /** - * We use a integer to record a key press. - * For instance, if the key HOME is pressed, key will be evaluated to - * `HOME' defined in keyboard.h. - */ - u32 key = 0; - - /** - * This var points to a row in keymap[]. I don't use two-dimension - * array because I don't like it. - */ - u32 *keyrow; - - while (kb_in.count > 0) - { - code_with_E0 = 0; - scan_code = get_byte_from_kb_buf(); - - /* parse the scan code below */ - if (scan_code == 0xE1) - { - int i; - u8 pausebreak_scan_code[] = {0xE1, 0x1D, 0x45, 0xE1, 0x9D, 0xC5}; - int is_pausebreak = 1; - for (i = 1; i < 6; i++) - { - if (get_byte_from_kb_buf() != pausebreak_scan_code[i]) - { - is_pausebreak = 0; - break; - } - } - if (is_pausebreak) - { - key = PAUSEBREAK; - } - } - else if (scan_code == 0xE0) - { - code_with_E0 = 1; - scan_code = get_byte_from_kb_buf(); - - /* PrintScreen is pressed */ - if (scan_code == 0x2A) - { - code_with_E0 = 0; - if ((scan_code = get_byte_from_kb_buf()) == 0xE0) - { - code_with_E0 = 1; - if ((scan_code = get_byte_from_kb_buf()) == 0x37) - { - key = PRINTSCREEN; - make = 1; - } - } - } - /* PrintScreen is released */ - else if (scan_code == 0xB7) - { - code_with_E0 = 0; - if ((scan_code = get_byte_from_kb_buf()) == 0xE0) - { - code_with_E0 = 1; - if ((scan_code = get_byte_from_kb_buf()) == 0xAA) - { - key = PRINTSCREEN; - make = 0; - } - } - } - } - - if ((key != PAUSEBREAK) && (key != PRINTSCREEN)) - { - int caps; - - /* make or break */ - make = (scan_code & FLAG_BREAK ? 0 : 1); - - keyrow = &keymap[(scan_code & 0x7F) * MAP_COLS]; - - column = 0; - - caps = shift_l || shift_r; - if (caps_lock && - keyrow[0] >= 'a' && keyrow[0] <= 'z') - caps = !caps; - - if (caps) - column = 1; - - if (code_with_E0) - column = 2; - - key = keyrow[column]; - - switch (key) - { - case SHIFT_L: - shift_l = make; - break; - case SHIFT_R: - shift_r = make; - break; - case CTRL_L: - ctrl_l = make; - break; - case CTRL_R: - ctrl_r = make; - break; - case ALT_L: - alt_l = make; - break; - case ALT_R: - alt_l = make; - break; - case CAPS_LOCK: - if (make) - { - caps_lock = !caps_lock; - set_leds(); - } - break; - case NUM_LOCK: - if (make) - { - num_lock = !num_lock; - set_leds(); - } - break; - case SCROLL_LOCK: - if (make) - { - scroll_lock = !scroll_lock; - set_leds(); - } - break; - default: - break; - } - } - - if (make) - { /* Break Code is ignored */ - int pad = 0; - - /* deal with the numpad first */ - if ((key >= PAD_SLASH) && (key <= PAD_9)) - { - pad = 1; - switch (key) - { /* '/', '*', '-', '+', - * and 'Enter' in num pad - */ - case PAD_SLASH: - key = '/'; - break; - case PAD_STAR: - key = '*'; - break; - case PAD_MINUS: - key = '-'; - break; - case PAD_PLUS: - key = '+'; - break; - case PAD_ENTER: - key = ENTER; - break; - default: - /* the value of these keys - * depends on the Numlock - */ - if (num_lock) - { /* '0' ~ '9' and '.' in num pad */ - if (key >= PAD_0 && key <= PAD_9) - key = key - PAD_0 + '0'; - else if (key == PAD_DOT) - key = '.'; - } - else - { - switch (key) - { - case PAD_HOME: - key = HOME; - break; - case PAD_END: - key = END; - break; - case PAD_PAGEUP: - key = PAGEUP; - break; - case PAD_PAGEDOWN: - key = PAGEDOWN; - break; - case PAD_INS: - key = INSERT; - break; - case PAD_UP: - key = UP; - break; - case PAD_DOWN: - key = DOWN; - break; - case PAD_LEFT: - key = LEFT; - break; - case PAD_RIGHT: - key = RIGHT; - break; - case PAD_DOT: - key = DELETE; - break; - default: - break; - } - } - break; - } - } - key |= shift_l ? FLAG_SHIFT_L : 0; - key |= shift_r ? FLAG_SHIFT_R : 0; - key |= ctrl_l ? FLAG_CTRL_L : 0; - key |= ctrl_r ? FLAG_CTRL_R : 0; - key |= alt_l ? FLAG_ALT_L : 0; - key |= alt_r ? FLAG_ALT_R : 0; - key |= pad ? FLAG_PAD : 0; - in_process(p_tty, key); - } - } -} -#endif static u16 map_key(int code) { @@ -487,16 +226,11 @@ static u16 map_key(int code) } else { - // if (sticky_alt_mode && (lk & ALT_LOCK)) { - // column = 2; - // if (caps) column = 4; - // } else { column = 0; if (caps) column = 1; if (ctrl) column = 5; - // } } return keyrow[column] & ~(HASNUM | HASCAPS); } @@ -564,16 +298,22 @@ static void kbd_process(unsigned char scode) alt_r = press; break; case INPUT_KEY_NUM_LOCK: - if (press) + if (press){ num_lock = !num_lock; + set_leds(); + } break; case INPUT_KEY_CAPS_LOCK: - if (press) + if (press){ caps_lock = !caps_lock; + set_leds(); + } break; case INPUT_KEY_SCROLL_LOCK: - if (press) + if (press){ scroll_lock = !scroll_lock; + set_leds(); + } break; } @@ -585,34 +325,6 @@ static void kbd_process(unsigned char scode) kbd_state = 0; } -/***************************************************************************** - * get_byte_from_kb_buf - *****************************************************************************/ -/** - * Read a byte from the keyboard buffer. - * - * @return The byte read. - *****************************************************************************/ -static u8 get_byte_from_kb_buf() -{ - u8 scan_code; - - while (kb_in.count <= 0) - { - } /* wait for a byte to arrive */ - - disable_int(); /* for synchronization */ - scan_code = *(kb_in.p_tail); - kb_in.p_tail++; - if (kb_in.p_tail == kb_in.buf + KB_IN_BYTES) - { - kb_in.p_tail = kb_in.buf; - } - kb_in.count--; - enable_int(); /* for synchronization */ - - return scan_code; -} /***************************************************************************** * kb_wait @@ -661,29 +373,34 @@ static void set_mouse_leds() outb(KB_DATA, KBC_MODE); } -static struct spinlock buflock; - #define LASTKEY(x) LAST(x, TTY_IN_BYTES) #define NEXTKEY(x) NEXT(x, TTY_IN_BYTES) -static u8 keybuf[3][TTY_IN_BYTES]; +// static u8 keybuf[3][TTY_IN_BYTES]; void ps2_tty_init(NTTY *tty) { static int _cnt = 0; assert(tty->driver_type == 1); assert(tty->input_buf); keyboard_buf *kbd = (keyboard_buf *)tty->input_buf; - kbd->buf = (void *)keybuf[_cnt++]; + // kbd->buf = (void *)keybuf[_cnt++]; + kbd->buf = (void*)K_PHY2LIN(do_kmalloc(TTY_IN_BYTES)); + // kprintf("kbd buf: %p\n", kbd->buf); kbd->head = kbd->tail = kbd->readable = 0; kbd->len = 0; - buflock.locked = 0; +} + +static inline void printallbuf(u8* ibuf) { + for (int i = 0; i < TTY_IN_BYTES; ++ i) { + kprintf("%c", ibuf[i]); + } + kprintf("\n"); } int ps2_tty_read(NTTY *tty, char *buf, int nr) { assert(tty->input_buf); keyboard_buf *kbd = (keyboard_buf *)tty->input_buf; - assert(kbd->buf); int i = 0; while (1) { @@ -693,11 +410,12 @@ int ps2_tty_read(NTTY *tty, char *buf, int nr) break; } } + assert(kbd->buf); u8 *ibuf = kbd->buf; for (; i < nr && i < kbd->readable; ++i) { - *(buf + i) = *(ibuf + kbd->head); - // kprintf("read %p\n", ibuf+kbd->head); + *buf ++ = ibuf[kbd->head]; + // kprintf("[r]%p;", ibuf+kbd->head); kbd->head = NEXTKEY(kbd->head); } kbd->readable -= i; @@ -721,6 +439,7 @@ static void ps2_push(NTTY *tty, u32 key) disable_int(); assert(tty->input_buf); keyboard_buf *kbd = (keyboard_buf *)tty->input_buf; + u8* buf = kbd->buf; if (key & ISMOUSE) { if (key & MOUSESCR) @@ -749,25 +468,14 @@ static void ps2_push(NTTY *tty, u32 key) { switch (key) { - case F1: - case F2: - case F3: - case F4: - case F5: - case F6: - case F7: - case F8: - case F9: - case F10: - case F11: - case F12: - // select_console((key & 0xff) - (F1 & 0xff)); // TODO + case F1: case F2: case F3: case F4: case F5: case F6: + case F7: case F8: case F9: case F10: case F11: case F12: { int conno = (key & 0xff) - (F1 & 0xff); if (conno < NR_CONSOLES) { - kprintf("select console %d\n", conno); - vga_tty_select(default_ntty + conno); + // kprintf("select console %d\n", conno); + vga_tty_select(get_tty(conno)); } break; } @@ -783,7 +491,7 @@ static void ps2_push(NTTY *tty, u32 key) case '\r': case '\n': // escape ENTER vga_tty_write(tty, '\n'); - ((u8 *)kbd->buf)[kbd->tail] = '\n'; + buf[kbd->tail] = '\n'; kbd->len++; kbd->tail = NEXTKEY(kbd->tail); kbd->readable = CYCLE_SUB(kbd->head, kbd->tail, TTY_IN_BYTES); @@ -804,7 +512,8 @@ static void ps2_push(NTTY *tty, u32 key) if (kbd->len == TTY_IN_BYTES - 1) return; // leave one space for ctrl ascii vga_tty_write(tty, key); - ((u8 *)kbd->buf)[kbd->tail] = key & 0xff; + buf[kbd->tail] = (u8)(key & 0xff); + // kprintf("%d %d %d", key & 0xff, kbd->tail, buf[kbd->tail]); kbd->len++; kbd->tail = NEXTKEY(kbd->tail); // kprintf("len=%d, h=%d, t=%d, rd=%d\n", kbd->len, kbd->head, kbd->tail, kbd->readable); @@ -814,5 +523,3 @@ static void ps2_push(NTTY *tty, u32 key) } enable_int(); } - -// TODO: 1. 修改键盘驱动 2. 添加测试代码 3. 添加鼠标并调试滚动 \ No newline at end of file diff --git a/kernel/main.c b/kernel/main.c index 09ce2c0..a523d6f 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -31,6 +31,8 @@ static int initialize_cpus(); // added by xw, 18/6/2 *======================================================================*/ int kernel_main() { + clear_kernel_pagepte_low(); + init_serial(); int error; @@ -114,7 +116,6 @@ int kernel_main() * note that disp_xx can't work after this function is invoked until processes runs. * add by visual 2016.5.13; moved by xw, 18/5/30 */ - clear_kernel_pagepte_low(); p_proc_current = proc_table; kernel_initial = 0; // kernel initialization is done. added by xw, 18/5/31 diff --git a/kernel/memman.c b/kernel/memman.c index b84c167..111e66e 100644 --- a/kernel/memman.c +++ b/kernel/memman.c @@ -27,7 +27,7 @@ void init() //初始化 u32 memstart = MEMSTART; //4M 开始初始化 u32 i,j; - memcpy(MemInfo,(u32 *)FMIBuff,1024); //复制内存 + memcpy(MemInfo,(u32 *)(K_PHY2LIN(FMIBuff)),1024); //复制内存 memman_init(memman); //初始化memman中frees,maxfrees,lostsize,losts @@ -329,77 +329,3 @@ u32 memman_total(struct MEMMAN *man) } return t; } - -void memman_test() -{ //测试 - u32 *p1 = 0; - u32 *p2 = 0; - u32 *p3 = 0; - u32 *p4 = 0; - u32 *p = 0; - - p1 = (u32 *)do_malloc(4); - if(-1 != (u32)p1){ //打印p1,当前空闲内存信息,p1所指内存的内容 - disp_str("START"); - disp_int((u32)p1); - //disp_free(); - *p1 = TEST; - disp_int(*p1); - disp_str("END"); - } - - p2 = (u32 *)do_kmalloc(4); - if(-1 != (u32)p2){ - disp_str("START"); - disp_int((u32)p2); - //disp_free(); - *p2 = TEST; - disp_int(*p2); - disp_str("END"); - } - - do_free((u32)p1,4); - do_free((u32)p2,4); - - p3 = (u32 *)do_malloc_4k(); - if(-1 != (u32)p3){ - disp_str("START"); - disp_int((u32)p3); - //disp_free(); - *p3 = TEST; - disp_int(*p3); - p = p3 + 2044; - *p = 0x22334455; - disp_int(*p); - p += 2048; - *p = 0x33445566; - disp_int(*p); - disp_str("END"); - } - - p4 = (u32 *)do_kmalloc_4k(4); - if(-1 != (u32)p4){ - disp_str("START"); - disp_int((u32)p4); - //disp_free(); - *p4 = TEST; - disp_int(*p4); - p = p4 + 2044; - *p = 0x22334455; - disp_int(*p); - p += 2048; - *p = 0x33445566; - disp_int(*p); - disp_str("END"); - } - - do_free_4k((u32)p3); - do_free_4k((u32)p4); - - disp_str("START"); - disp_free(); - disp_str("END"); - - return; -} - diff --git a/kernel/pagetbl.c b/kernel/pagetbl.c index 62427ca..8b628e2 100644 --- a/kernel/pagetbl.c +++ b/kernel/pagetbl.c @@ -10,6 +10,8 @@ #include "global.h" #include "proto.h" #include "memman.h" +#include "stdio.h" +#include "x86.h" // to determine if a page fault is reparable. added by xw, 18/6/11 u32 cr2_save; @@ -81,18 +83,9 @@ void page_fault_handler(u32 vec_no, //异常编号,此时应该是14,代 // if page fault happens in kernel, it's an error. if (kernel_initial == 1) { - disp_str("\n"); - disp_color_str("Page Fault\n", 0x74); - disp_color_str("eip=", 0x74); //灰底红字 - disp_int(eip); - disp_color_str("eflags=", 0x74); - disp_int(eflags); - disp_color_str("cs=", 0x74); - disp_int(cs); - disp_color_str("err_code=", 0x74); - disp_int(err_code); - disp_color_str("Cr2=", 0x74); //灰底红字 - disp_int(cr2); + kprintf("\n"); + kprintf("\033[91;47m Page Fault\n\033[0m"); + kprintf("\033[91meip=0x%08x, eflags=0x%x, CS=0x%x, ERR=0x%x, CR2=0x%08x\033[0m\n", eip, eflags, cs, err_code, cr2); halt(); } @@ -101,51 +94,27 @@ void page_fault_handler(u32 vec_no, //异常编号,此时应该是14,代 //获取该线性地址对应的页表的物理地址 pte_addr_phy_temp = get_pte_phy_addr(p_proc_current->task.pid, cr2); - if (cr2 == cr2_save) - { - cr2_count++; - if (cr2_count == 5) - { - disp_str("\n"); - disp_color_str("Page Fault\n", 0x74); - disp_color_str("eip=", 0x74); //灰底红字 - disp_int(eip); - disp_color_str("eflags=", 0x74); - disp_int(eflags); - disp_color_str("cs=", 0x74); - disp_int(cs); - disp_color_str("err_code=", 0x74); - disp_int(err_code); - disp_color_str("Cr2=", 0x74); //灰底红字 - disp_int(cr2); - disp_color_str("Cr3=", 0x74); - disp_int(p_proc_current->task.cr3); - //获取页目录中填写的内容 - disp_color_str("Pde=", 0x74); - disp_int(*((u32 *)K_PHY2LIN(pde_addr_phy_temp) + get_pde_index(cr2))); - //获取页表中填写的内容 - disp_color_str("Pte=", 0x74); - disp_int(*((u32 *)K_PHY2LIN(pte_addr_phy_temp) + get_pte_index(cr2))); - halt(); - } - } - else - { - cr2_save = cr2; - cr2_count = 0; - } + kprintf("\n"); + kprintf("\033[91;47m Page Fault\033[0m in %d\n", p_proc_current->task.pid); + kprintf("\033[91meip=0x%08x, eflags=0x%x, CS=0x%x, ERR=0x%x, CR2=0x%08x\033[0m\n", eip, eflags, cs, err_code, cr2); + kprintf("\033[91mCR3=0x%08x, PDE=%p, PTE=%p\033[0m\n", + p_proc_current->task.cr3, + *((u32 *)K_PHY2LIN(pde_addr_phy_temp) + get_pde_index(cr2)), + *((u32 *)K_PHY2LIN(pte_addr_phy_temp) + get_pte_index(cr2))); + disable_int(); + halt(); if (0 == pte_exist(pde_addr_phy_temp, cr2)) { //页表不存在 - // disp_color_str("[Pde Fault!]",0x74); //灰底红字 + // kprintf("\033[91;47m[PDE FAULT]\033[0m in %d\n",p_proc_current->task.pid); (*((u32 *)K_PHY2LIN(pde_addr_phy_temp) + get_pde_index(cr2))) |= PG_P; - // disp_color_str("[Solved]",0x74); + // kprintf("\033[92;47m[Solved]\033[0m in %d\n",p_proc_current->task.pid); } else { //只是缺少物理页 - // disp_color_str("[Pte Fault!]",0x74); //灰底红字 + // kprintf("\033[91;47m[PTE FAULT]\033[0m in %d\n",p_proc_current->task.pid); (*((u32 *)K_PHY2LIN(pte_addr_phy_temp) + get_pte_index(cr2))) |= PG_P; - // disp_color_str("[Solved]",0x74); + // kprintf("\033[92;47m[Solved]\033[0m in %d\n",p_proc_current->task.pid); } refresh_page_cache(); } diff --git a/kernel/start.c b/kernel/start.c index 9d8fe19..d456e6b 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -57,7 +57,7 @@ _warn(const char *file, int line, const char *fmt,...) *======================================================================*/ void cstart() { - kprintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-----\"cstart\" begins-----\n"); + kprintf("-----\"cstart\" begins-----\n"); // 将 LOADER 中的 GDT 复制到新的 GDT 中 memcpy( &gdt, // New GDT diff --git a/kernel/tty.c b/kernel/tty.c index dfc23ea..3dacd67 100644 --- a/kernel/tty.c +++ b/kernel/tty.c @@ -14,10 +14,9 @@ int current_console; // 当前显示在屏幕上的console -void tty_write(TTY *tty, char *buf, int len); -int tty_read(TTY *tty, char *buf, int len); +void tty_write(NTTY *tty, char *buf, int len); +int tty_read(NTTY *tty, char *buf, int len); -static void init_tty(TTY *tty); static void tty_mouse(TTY *tty); static void tty_dev_read(TTY *tty); static void tty_dev_write(TTY *tty); @@ -26,18 +25,22 @@ static void put_key(TTY *tty, u32 key); #ifdef DEBUGNEW NTTY* cur_ntty; -NTTY* default_ntty; -NTTY ntty_table[NR_CONSOLES]; +NTTY* ntty_table[NR_CONSOLES]; // int cur_ntty = 0; static keyboard_buf keyboardbuf[NR_CONSOLES]; static vga_buf vgabuf[NR_CONSOLES]; +inline NTTY* get_tty(const int nr_tty) { + return ntty_table[nr_tty]; +} + void init_tty_main() { NTTY *tty; for (int i = 0; i < 3; ++i) { - tty = &ntty_table[i]; + // tty = &ntty_table[i]; + tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY))); tty->driver_type = 1; // vga // tty->input_buf = (void*)do_kmalloc(sizeof(keyboard_buf)); // tty->output_buf = (void*)do_kmalloc(sizeof(vga_buf)); @@ -45,8 +48,10 @@ void init_tty_main() tty->output_buf = &vgabuf[i]; vga_tty_init(tty); ps2_tty_init(tty); + ntty_table[i] = tty; + // kprintf("tty: %p, outbuf: %p\n", tty, tty->output_buf); } - cur_ntty = default_ntty = &ntty_table[0]; + cur_ntty = ntty_table[0]; } #endif @@ -115,185 +120,21 @@ void in_process(TTY *p_tty, u32 key) } #endif -#define TTY_FIRST (tty_table) -#define TTY_END (tty_table + NR_CONSOLES) void task_tty() { -#ifdef DEBUGNEW // NTTY* p_tty; // for (p_tty = ntty_table; p_tty < ntty_table + 3; ++ p_tty) { // init_ntty(p_tty); // } while (1) { + // sys_yield(); // vga_tty_flush(cur_ntty); } -#else - TTY *p_tty; - for (p_tty = TTY_FIRST; p_tty < TTY_END; p_tty++) - { - init_tty(p_tty); - } - p_tty = tty_table; - - select_console(0); - - // 设置第一个tty光标位置,第一个tty需要特殊处理 - disable_int(); - outb(CRTC_ADDR_REG, CURSOR_H); - outb(CRTC_DATA_REG, ((disp_pos / 2) >> 8) & 0xFF); - outb(CRTC_ADDR_REG, CURSOR_L); - outb(CRTC_DATA_REG, (disp_pos / 2) & 0xFF); - enable_int(); - - // 轮询 - while (1) - { - for (p_tty = TTY_FIRST; p_tty < TTY_END; p_tty++) - { - do - { - tty_mouse(p_tty); /* tty判断鼠标操作 */ - tty_dev_read(p_tty); /* 从键盘输入缓冲区读到这个tty自己的缓冲区 */ - tty_dev_write(p_tty); /* 把tty缓存区的数据写到这个tty占有的显存 */ - - } while (p_tty->ibuf_cnt); - } - } -#endif } -static void init_tty(TTY *p_tty) -{ - p_tty->ibuf_read_cnt = p_tty->ibuf_cnt = 0; - p_tty->status = TTY_STATE_DISPLAY; - p_tty->ibuf_read = p_tty->ibuf_head = p_tty->ibuf_tail = p_tty->ibuf; - int det = p_tty - tty_table; - p_tty->console = console_table + det; - - p_tty->mouse_left_button = 0; - p_tty->mouse_mid_button = 0; - p_tty->mouse_X = 0; - p_tty->mouse_Y = 0; - init_screen(p_tty); -} - -static void tty_mouse(TTY *tty) -{ - if (is_current_console(tty->console)) - { - int real_line = tty->console->orig / SCR_WIDTH; - if (tty->mouse_left_button) - { - - if (tty->mouse_Y > MOUSE_UPDOWN_BOUND) - { // 按住鼠标左键向上滚动 - if (tty->console->current_line < 43) - { - disable_int(); - tty->console->current_line++; - outb(CRTC_ADDR_REG, START_ADDR_H); - outb(CRTC_DATA_REG, ((80 * (tty->console->current_line + real_line)) >> 8) & 0xFF); - outb(CRTC_ADDR_REG, START_ADDR_L); - outb(CRTC_DATA_REG, (80 * (tty->console->current_line + real_line)) & 0xFF); - enable_int(); - tty->mouse_Y = 0; - } - } - else if (tty->mouse_Y < -MOUSE_UPDOWN_BOUND) - { // 按住鼠标左键向下滚动 - if (tty->console->current_line > 0) - { - disable_int(); - tty->console->current_line--; - outb(CRTC_ADDR_REG, START_ADDR_H); - outb(CRTC_DATA_REG, ((80 * (tty->console->current_line + real_line)) >> 8) & 0xFF); - outb(CRTC_ADDR_REG, START_ADDR_L); - outb(CRTC_DATA_REG, (80 * (tty->console->current_line + real_line)) & 0xFF); - enable_int(); - tty->mouse_Y = 0; - } - } - } - - if (tty->mouse_mid_button) - { // 点击中键复原 - disable_int(); - tty->console->current_line = 0; - outb(CRTC_ADDR_REG, START_ADDR_H); - outb(CRTC_DATA_REG, ((80 * (tty->console->current_line + real_line)) >> 8) & 0xFF); - outb(CRTC_ADDR_REG, START_ADDR_L); - outb(CRTC_DATA_REG, (80 * (tty->console->current_line + real_line)) & 0xFF); - enable_int(); - tty->mouse_Y = 0; - } - } -} -#ifndef DEBUGNEW -static void tty_dev_read(TTY *tty) -{ - if (is_current_console(tty->console)) - { - keyboard_read(tty); - } -} - -static void tty_dev_write(TTY *tty) -{ - - if (tty->ibuf_cnt) - { - char ch = *(tty->ibuf_tail); - tty->ibuf_tail++; - if (tty->ibuf_tail == tty->ibuf + TTY_IN_BYTES) - { - tty->ibuf_tail = tty->ibuf; - } - tty->ibuf_cnt--; - - if (ch == '\b') - { - if (tty->ibuf_read_cnt == 1) - { - tty->ibuf_read_cnt--; - tty->ibuf_head--; - tty->ibuf_tail--; - return; - } - else - { - tty->ibuf_read_cnt -= 2; - if (tty->ibuf_head == tty->ibuf) - { - tty->ibuf_head = tty->ibuf_tail = &tty->ibuf[256 - 2]; - } - else - { - tty->ibuf_head--; - tty->ibuf_tail--; - tty->ibuf_head--; - tty->ibuf_tail--; - } - } - } - out_char(tty->console, ch); - } -} -#endif -static void put_key(TTY *tty, u32 key) -{ - if (tty->ibuf_cnt < TTY_IN_BYTES) - { - *(tty->ibuf_head) = key; - tty->ibuf_head++; - if (tty->ibuf_head == tty->ibuf + TTY_IN_BYTES) - tty->ibuf_head = tty->ibuf; - tty->ibuf_cnt++; - tty->ibuf_read_cnt++; - } -} /***************************************************************************** * tty_write @@ -301,18 +142,13 @@ static void put_key(TTY *tty, u32 key) * 当fd=STD_OUT时,write()系统调用转发到此函数 *****************************************************************************/ -void tty_write(TTY *tty, char *buf, int len) +void tty_write(NTTY *tty, char *buf, int len) { -#ifdef DEBUGNEW while (--len >= 0) { - vga_tty_write(cur_ntty, *buf++); + vga_tty_write(tty, *buf++); } - vga_tty_flush(cur_ntty); -#else - while (--len >= 0) - out_char(tty->console, *buf++); -#endif + if (cur_ntty == tty) vga_tty_flush(tty); } /***************************************************************************** @@ -321,32 +157,7 @@ void tty_write(TTY *tty, char *buf, int len) * 当fd=STD_IN时,read()系统调用转发到此函数 *****************************************************************************/ -int tty_read(TTY *tty, char *buf, int len) +int tty_read(NTTY *tty, char *buf, int len) { -#ifdef DEBUGNEW - return ps2_tty_read(cur_ntty, buf, len); -#else - int i = 0; - if (!tty->ibuf_read_cnt) - { - tty->status |= TTY_STATE_WAIT_ENTER; - } - - while ((tty->status & TTY_STATE_WAIT_ENTER)) - ; - - while (tty->ibuf_read_cnt && i < len) - { - buf[i] = *tty->ibuf_read; - tty->ibuf_read++; - if (tty->ibuf_read == tty->ibuf + TTY_IN_BYTES) - { - tty->ibuf_read = tty->ibuf; - } - tty->ibuf_read_cnt--; - i++; - } - - return i; -#endif + return ps2_tty_read(tty, buf, len); } \ No newline at end of file diff --git a/kernel/vfs.c b/kernel/vfs.c index 0d10214..e837c05 100644 --- a/kernel/vfs.c +++ b/kernel/vfs.c @@ -137,6 +137,7 @@ static void init_vfs_table() // tty0 // device_table[0].dev_name="dev_tty0"; // device_table[0].op = &f_op_table[0]; + memset(vfs_table, 0, sizeof(vfs_table)); vfs_table[0].fs_name = "dev_tty0"; // modifed by mingxuan 2020-10-18 vfs_table[0].op = &f_op_table[0]; vfs_table[0].sb = &super_block[0]; // 每个tty都有一个superblock //added by mingxuan 2020-10-30 @@ -208,7 +209,9 @@ static int get_index(char path[]) for (i = 0; i < NR_FS; i++) // modified by mingxuan 2020-10-29 { // if(!strcmp(dev_name, device_table[i].dev_name)) + if(vfs_table[i].fs_name == NULL) continue; if (!strcmp(fs_name, vfs_table[i].fs_name)) // modified by mingxuan 2020-10-18 + // f**k u mingxuan, you bloody retarded idiot piece of shit return i; } @@ -303,7 +306,7 @@ int do_vopen(const char *path, int flags) } else { - kprintf(" error!\n"); + kprintf("open %s error!\n", path); } return fd; diff --git a/user/Makefrag b/user/Makefrag index 187ffda..404a348 100644 --- a/user/Makefrag +++ b/user/Makefrag @@ -6,9 +6,10 @@ USERLIB_OBJS := $(patsubst %.c, $(OBJDIR)/%.o, $(USERLIB_SRCS)) USERLIB_OBJS := $(patsubst %.asm, $(OBJDIR)/%.o, $(USERLIB_OBJS)) # 这里给我整不会了,文件名只能长度为16位,否则会寄,原因还挺难找 -USER_SRCS := user/ptTest.c \ +USER_SRCS := user/shell_1.c \ user/shell_0.c \ user/test.c \ + user/test2.c \ USER_BINS := $(patsubst %.c, $(OBJDIR)/%.bin, $(USER_SRCS)) USER_BASENAMES := $(patsubst $(OBJDIR)/user/%, %, $(USER_BINS)) diff --git a/user/shell_1.c b/user/shell_1.c new file mode 100644 index 0000000..21b189a --- /dev/null +++ b/user/shell_1.c @@ -0,0 +1,54 @@ +#include "type.h" +#include "const.h" +#include "protect.h" +#include "string.h" +#include "proc.h" +#include "global.h" +#include "proto.h" +#include "stdio.h" + +int main(int arg, char *argv[]) +{ + if (0 == fork()) { + int stdin = open("dev_tty0", O_RDWR); + int stdout = open("dev_tty0", O_RDWR); + int stderr = open("dev_tty0", O_RDWR); + + char buf[1024]; + int pid; + int times = 0; + while (1) + { + printf("\nminiOS:/ $ "); + if (gets(buf) && strlen(buf) != 0) + { + if (exec(buf) != 0) + { + printf("exec failed: file not found!\n"); + continue; + } + } + } + } + else { + int stdin = open("dev_tty1", O_RDWR); + int stdout = open("dev_tty1", O_RDWR); + int stderr = open("dev_tty1", O_RDWR); + + char buf[1024]; + int pid; + int times = 0; + while (1) + { + printf("\nminiOS:/ $ "); + if (gets(buf) && strlen(buf) != 0) + { + if (exec(buf) != 0) + { + printf("exec failed: file not found!\n"); + continue; + } + } + } + } +} \ No newline at end of file diff --git a/user/test2.c b/user/test2.c new file mode 100644 index 0000000..b3388b2 --- /dev/null +++ b/user/test2.c @@ -0,0 +1,40 @@ +#include "type.h" +#include "const.h" +#include "protect.h" +#include "string.h" +#include "proc.h" +#include "global.h" +#include "proto.h" +#include "stdio.h" + +int main(int arg, char *argv[]) +{ + int pid = fork(); + volatile int i; + if (pid == 0) { + int stdin = open("dev_tty0", O_RDWR); + int stdout = open("dev_tty0", O_RDWR); + int stderr = open("dev_tty0", O_RDWR); + + while(1) { + printf("\x1b[32;40mAAA "); + i=100000000; + while(--i); + } + } + else { + int stdin = open("dev_tty1", O_RDWR); + int stdout = open("dev_tty1", O_RDWR); + int stderr = open("dev_tty1", O_RDWR); + + while(1) { + char* s = "\x1b[33;40mBBB "; + for (char* p = s; *p != 0; p++ ){ + write(stdout, p, 1); + } + i=100000000; + while(--i); + } + } + return 0; +} \ No newline at end of file