diff --git a/include/common.h b/include/common.h index 759ab93..95607f8 100644 --- a/include/common.h +++ b/include/common.h @@ -2,7 +2,7 @@ #define CRASHPOWEROS_COMMON_H #define OS_NAME "CrashPowerDOS" -#define OS_VERSION "v0.2.0" +#define OS_VERSION "v0.2.1" #include #include diff --git a/include/memory.h b/include/memory.h index 0ef8493..af5e5ad 100644 --- a/include/memory.h +++ b/include/memory.h @@ -6,7 +6,7 @@ #include "isr.h" #define KHEAP_INITIAL_SIZE 0xf00000 -#define KHEAP_START 0xc0000000 +#define KHEAP_START 0xc0000000 #define STACK_SIZE 32768 #define INDEX_FROM_BIT(a) (a / (8*4)) @@ -43,6 +43,8 @@ typedef union header { ALIGN stub; } header_t; +uint32_t memory_usage(); + void *memcpy(void *dst_, const void *src_, uint32_t size); int memcmp(const void *a_, const void *b_, uint32_t size); diff --git a/include/shell.h b/include/shell.h index 59b9066..2e52337 100644 --- a/include/shell.h +++ b/include/shell.h @@ -12,5 +12,11 @@ void setup_shell(); void cmd_echo(int argc,char **argv); void cmd_proc(); void cmd_date(); +void cmd_reset(); +void cmd_del(int argc, char **argv); +void cmd_mkdir(int argc, char **argv); +void cmd_read(int argc, char **argv); +void cmd_cat(int argc, char **argv); +void cmd_ls(); #endif //CRASHPOWEROS_SHELL_H diff --git a/include/task.h b/include/task.h index 34544a4..97c4ebf 100644 --- a/include/task.h +++ b/include/task.h @@ -21,17 +21,13 @@ struct context { uint32_t eflags; }; -struct mm_struct { - page_directory_t *pgd_dir; // 进程页表 -}; - // 进程控制块 PCB struct task_struct { volatile task_state state; // 进程当前状态 int pid; // 进程标识符 char *name; // 进程名 void *stack; // 进程的内核栈地址 - struct mm_struct *mm; // 当前进程的内存地址映像 + page_directory_t *pgd_dir; // 进程页表 struct context context; // 进程切换需要的上下文信息 struct task_struct *next; // 链表指针 }; diff --git a/kernel/kernel.c b/kernel/kernel.c index 916d328..03fdada 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -13,7 +13,6 @@ extern uint32_t end; uint32_t placement_address = (uint32_t) & end; - void kernel_main() { io_cli(); vga_install(); @@ -28,6 +27,7 @@ void kernel_main() { printf("[kernel]: PCB load success!\n"); init_keyboard(); printf("[kernel]: Keyboard driver load success!\n"); + print_cpu_id(); io_sti(); diff --git a/kernel/kheap.c b/kernel/kheap.c index 42a6a7a..2e7f491 100644 --- a/kernel/kheap.c +++ b/kernel/kheap.c @@ -6,6 +6,18 @@ extern uint32_t end; // declared in linker.ld static uint32_t placement_address = (uint32_t) &end; void *program_break, *program_break_end; +uint32_t memory_usage(){ + header_t *curr = head; + uint32_t size; + while (curr) { + if(!curr->s.is_free){ + size += curr->s.size; + } + curr = curr->s.next; + } + return size; +} + static uint32_t kmalloc_int(uint32_t sz, uint32_t align, uint32_t *phys) { if (program_break) { // 有内存堆 @@ -24,6 +36,7 @@ static uint32_t kmalloc_int(uint32_t sz, uint32_t align, uint32_t *phys) { if (phys) *phys = placement_address; uint32_t tmp = placement_address; placement_address += sz; + return tmp; } diff --git a/kernel/task.c b/kernel/task.c index ce929df..f6122e1 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -6,6 +6,8 @@ struct task_struct *running_proc_head = NULL; struct task_struct *wait_proc_head = NULL; struct task_struct *current = NULL; +extern page_directory_t *kernel_directory; + extern void switch_to(struct context *prev, struct context *next); int now_pid = 0; @@ -62,7 +64,10 @@ void print_proc(){ void schedule() { if (current) { - change_task_to(current->next); + volatile task_state state = current->next->state; + if(state == TASK_RUNNABLE || state == TASK_SLEEPING ){ + change_task_to(current->next); + } } } @@ -70,6 +75,7 @@ void change_task_to(struct task_struct *next) { if (current != next) { struct task_struct *prev = current; current = next; + //switch_page_directory(current->pgd_dir); switch_to(&(prev->context), &(current->context)); } } @@ -84,7 +90,8 @@ int32_t kernel_thread(int (*fn)(void *), void *arg,char* name) { new_task->state = TASK_RUNNABLE; new_task->stack = current; new_task->pid = now_pid++; - new_task->mm = NULL; + new_task->pgd_dir = (page_directory_t *) kmalloc_a(sizeof(page_directory_t)); + new_task->name = name; uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE); @@ -125,7 +132,7 @@ void init_sched() { current->state = TASK_RUNNABLE; current->pid = now_pid++; current->stack = current; // 该成员指向栈低地址 - current->mm = NULL; // 内核线程不需要该成员 + current->pgd_dir = kernel_directory; current->name = "CPOS-System"; current->next = current; diff --git a/sysapp/date.c b/sysapp/date.c index 7733a4c..c722013 100644 --- a/sysapp/date.c +++ b/sysapp/date.c @@ -13,7 +13,7 @@ int setup_date(){ clock_sleep(5); date_info = get_date_time(); //11 i = 0; - for(size_t x = VGA_WIDTH - 17; x < VGA_WIDTH ; x++){ + for(size_t x = VGA_WIDTH - 19; x < VGA_WIDTH ; x++){ const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x; terminal_buffer[index] = vga_entry(date_info[i],terminal_color); i++; diff --git a/sysapp/shell.c b/sysapp/shell.c index 64d5127..72410ca 100644 --- a/sysapp/shell.c +++ b/sysapp/shell.c @@ -2,17 +2,17 @@ #include "../include/queue.h" #include "../include/vga.h" #include "../include/common.h" -#include "../include/io.h" #include "../include/task.h" #include "../include/cmos.h" #include "../include/fat16.h" +#include "../include/timer.h" +#include "../include/io.h" extern Queue *key_char_queue; char getc() { while (key_char_queue->size == 0x00) { - io_hlt(); - + printf(""); } return queue_pop(key_char_queue); } @@ -73,7 +73,9 @@ void cmd_proc(){ void cmd_date(){ printf("System Time: %s\n",get_date_time()); + printf("Memory Usage: %dKB | All Size: %dMB\n",memory_usage()/1024,(KHEAP_START+KHEAP_INITIAL_SIZE)/1024/1024); print_cpu_id(); + vga_writestring("\n"); } void cmd_ls() { @@ -161,6 +163,12 @@ void cmd_del(int argc, char **argv) { kfree(info); } +void cmd_reset(){ + printf("Restart %s for x86..."); + clock_sleep(10); + outb(0x64,0xfe); +} + void setup_shell(){ vga_clear(); printf("%s for x86 [Version %s] \n",OS_NAME, OS_VERSION); @@ -200,6 +208,8 @@ void setup_shell(){ cmd_mkdir(argc, argv); else if (!strcmp("del", argv[0]) || !strcmp("rm", argv[0])) cmd_del(argc, argv); + else if (!strcmp("reset", argv[0])) + cmd_reset(); else if (!strcmp("help", argv[0]) || !strcmp("?", argv[0]) || !strcmp("h", argv[0])) { vga_writestring("-=[CrashPowerShell Helper]=-\n"); vga_writestring("help ? h Print shell help info.\n"); @@ -212,6 +222,7 @@ void setup_shell(){ vga_writestring("del rm Delete a file\n"); vga_writestring("sysinfo Print system info.\n"); vga_writestring("proc Lists all running processes.\n"); + vga_writestring("reset Reset OS.\n"); } else printf("[Shell]: Unknown command '%s'.\n", argv[0]); } }