From: AceVest Date: Thu, 31 Jul 2014 16:37:24 +0000 (+0800) Subject: add simple shell X-Git-Tag: 0.3.0~4 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=b0956dbd21fc5df67d25523aba21a683a7c735d5;p=kernel.git add simple shell --- diff --git a/bin/shell.c b/bin/shell.c index 746ff2b..5b62fd5 100644 --- a/bin/shell.c +++ b/bin/shell.c @@ -18,20 +18,40 @@ int sysdebug(); int pause(unsigned long tick); int main() { + + while(1) + { + printf("shell#"); + char buf[256]; + read(0, buf, 256); + + int pid = fork(); + if(pid > 0) + { + wait(pid); + } + else + { + execv(buf, 0); + } + } + + + int pid = fork(); printf("pid %u\n", pid); if(pid > 0) { + printf("parent. child pid %u\n", pid); while(1) { - printf("parent. child pid %u\n", pid); systest(); sysdebug(0x44444444); } } else { + printf("child\n"); while(1) { - printf("child\n"); systest(); sysdebug(0xAABBCCDD); } diff --git a/drivers/console.c b/drivers/console.c index 045571d..f2f7ea3 100644 --- a/drivers/console.c +++ b/drivers/console.c @@ -82,10 +82,13 @@ void cnsl_init() int cnsl_kbd_write(char ch) { + if(ch == 0) + return 0; + if(ch == '\b') { if(!empty(&cnsl.wr_q)) - vga_putc(0, '\b', 0x2); + vga_putc(0, '\b', 0xF); erase(&cnsl.wr_q); erase(&cnsl.sc_q); } @@ -93,7 +96,7 @@ int cnsl_kbd_write(char ch) { put(&cnsl.wr_q, ch); put(&cnsl.sc_q, ch); - vga_putc(0, ch, 0x2); + vga_putc(0, ch, 0xF); } @@ -104,20 +107,6 @@ int cnsl_kbd_write(char ch) put(&cnsl.rd_q, ch); wake_up(&rdwq); } - -#if 0 - printl(23, "rd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", - cnsl.rd_q.data[0], - cnsl.rd_q.data[1], - cnsl.rd_q.data[2], - cnsl.rd_q.data[3], - cnsl.rd_q.data[4], - cnsl.rd_q.data[5], - cnsl.rd_q.data[6], - cnsl.rd_q.data[7], - cnsl.rd_q.data[8], - cnsl.rd_q.data[9]); -#endif } @@ -143,7 +132,8 @@ int cnsl_read(char *buf, size_t count) if(r) { - buf[cnt++] = ch; + if(ch != '\n') + buf[cnt++] = ch; task->state = TASK_RUNNING; del_wait_queue(&rdwq, &wait); diff --git a/drivers/console.h b/drivers/console.h index 26ba40c..a51fe67 100644 --- a/drivers/console.h +++ b/drivers/console.h @@ -11,7 +11,7 @@ #include -#define CNSL_QUEUE_SIZE 10 +#define CNSL_QUEUE_SIZE 1024 typedef struct cnsl_queue { diff --git a/fs/ext2.c b/fs/ext2.c index f29e731..06c060e 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -54,7 +54,7 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) void *blk = ext2_alloc_block(); assert(blk != 0); - printk("read_inode %u\n", ino); + printd("read_inode %u\n", ino); unsigned int in; // inode number unsigned int gn; // group number @@ -75,7 +75,7 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) memcpy(inode, blk+inoff, sizeof(ext2_inode_t)); - printk(" inode size %u \n", inode->i_size); + printd(" inode size %u \n", inode->i_size); ext2_free_block(blk); } @@ -115,12 +115,12 @@ void ext2_read_data(const ext2_inode_t *inode, unsigned int offset, size_t size, assert(inode->i_size > 0 && inode->i_size<=MAX_SUPT_FILE_SIZE); assert(offset+size <= inode->i_size); assert(offset % EXT2_BLOCK_SIZE == 0); // for easy - printk("offset %x size %x %x\n", offset, size, offset+size); + printd("offset %x size %x %x\n", offset, size, offset+size); assert((offset+size) % EXT2_BLOCK_SIZE == 0); unsigned int blkid = offset / EXT2_BLOCK_SIZE; unsigned int blkcnt = size / EXT2_BLOCK_SIZE; - printk("id %u cnt %u\n", blkid, blkcnt); + printd("id %u cnt %u\n", blkid, blkcnt); BLKRW(inode->i_block[blkid], blkcnt, buf); } @@ -191,7 +191,7 @@ unsigned int ext2_search_inpath(const char *path) while((len=get_filename_from_path(path, file)) != 0) { ino = ext2_search_indir(file, inode); - assert(ino != 0); + //assert(ino != 0); path += len; @@ -257,7 +257,7 @@ void ext2_setup_fs() } ext2_read_inode(2, &ext2_root_inode); -#if 1 +#if 0 printk("root inode.i_size %u \n", ext2_root_inode.i_size); printk("root blocks %u \n", ext2_root_inode.i_blocks); diff --git a/kernel/exit.c b/kernel/exit.c index fc1ce73..9459879 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -12,15 +12,17 @@ #include #include +#include int sysc_exit(int status) { - // simple implement. + unsigned long flags; + irq_save(flags); current->state = TASK_EXITING; task_union *t = current; - - schedule(); + + irq_restore(flags); return 0; } diff --git a/kernel/fork.c b/kernel/fork.c index 968b75f..8448f19 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -83,12 +83,6 @@ int do_fork(pt_regs_t *regs, unsigned long flags) printk("----pde[%u] pte_src[%u] %08x\n", i, j, pte_src[j]); page_t *page = pa2page(pte_src[j]); page->count ++; - //assert(page->count <= 1); - if(page->count > 1) - { - printk("page %08x count %d\n", page, page->count); - panic("----"); - } } } diff --git a/kernel/sched.c b/kernel/sched.c index 381c098..7e7cd41 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -145,7 +145,7 @@ task_union *find_task(pid_t pid) unsigned long schedule() { static turn = 0; - task_union *sel = &root_task; + task_union *sel = 0; task_union *p = 0; list_head_t *pos = 0, *t=0; @@ -155,6 +155,8 @@ unsigned long schedule() { p = list_entry(pos, task_union, list); + printl(MPL_ROOT+p->pid, "task:%d [%08x] state %02x TURN %d turn %d cnt %u", p->pid, p, p->state, turn, p->weight, p->cnt); + if(p->state != TASK_RUNNING) { continue; @@ -162,21 +164,22 @@ unsigned long schedule() printd("%08x weight %d\n", p, p->weight); - if(p->weight != turn) + + if(sel == 0 && p->weight != turn) { p->weight = turn; sel = p; - break; } } irq_restore(iflags); - if(sel == &root_task) + + if(sel == 0) { + sel = &root_task; turn ++; } - printl(MPL_ROOT+sel->pid, "task:%d [%08x] turn %d cnt %u", sel->pid, sel, sel->weight, sel->cnt); task_union *prev = current; task_union *next = sel; diff --git a/kernel/setup.c b/kernel/setup.c index f3e623f..2c2963a 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -51,8 +51,9 @@ const char *version = "Kernel " VERSION " Build on "__DATE__ " " __TIME__ - " by " - BUIDER; + " @ " + BUIDER + "\n"; void setup_kernel() { @@ -93,7 +94,8 @@ void setup_kernel() setup_fs(); - printk("%s\n", version); - //vga_dbg_toggle(); + vga_puts(0, version, 0x2F); + + switch_printk_screen(); } diff --git a/kernel/wait.c b/kernel/wait.c index 40ac35c..9278067 100644 --- a/kernel/wait.c +++ b/kernel/wait.c @@ -96,15 +96,23 @@ int sysc_wait(unsigned long pid) if(p == 0) return 0; + if(p->state == TASK_EXITING) + return 0; + task_union * task = current; DECLARE_WAIT_QUEUE(wait, task); add_wait_queue(&p->wait, &wait); while(true) { - task->state = TASK_WAIT; + //task->state = TASK_WAIT; - if(p->state == TASK_EXITING) // no need irq_save + unsigned long flags; + irq_save(flags); + unsigned int state = p->state; + irq_restore(flags); + + if(state == TASK_EXITING) // no need irq_save break; schedule();