*.diff
bin/hw
bin/sh
+a.*
.global kernel_entry
.extern CheckKernel
.extern SetupKernel
-.extern KernelEntry
+.extern setup_kernel
.extern init_pgd
.extern init_pgt
.extern kernel_virtual_addr_start
call CheckKernel
addl $8,%esp
movl $root_task + TASK_SIZE, %esp
- call KernelEntry
+ call setup_kernel
- xorl %eax, %eax
- sti
- pushfl
- movw %cs, %ax
+
+#if 0
+ movl $0x23, %eax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
pushl %eax
+ pushl $KRNLADDR
+ pushl $0x282
+ pushl $0x1B
movl $root_task_entry, %eax
pushl %eax
iret
+#else
+ movl $root_task_entry, %eax
+ jmpl *%eax
+#endif
Die:
jmp Die # Should never come to here.
extern task_union root_task;
+#define FORK_KRNL 1
+
#endif //_SCHED_H
u32 eflags;
u32 _esp;
u16 ss, _ss;
-} pt_regs_t;
+} __attribute__((packed)) pt_regs_t;
typedef unsigned long Dev, *pDev;
/* 准备内核栈的数据并从ret_from_fork返回 */
pt_regs_t * regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1;
- extern void ret_from_fork();
+ extern void ret_from_fork_user();
memset((void*)regs, 0, sizeof(pt_regs_t));
regs->ss = SELECTOR_USER_DS;
regs->ds = SELECTOR_USER_DS;
regs->cs = SELECTOR_USER_CS;
regs->eip = (unsigned long)ehdr->e_entry;
current->esp = (unsigned long) regs;
- current->eip = (unsigned long)ret_from_fork;
+ current->eip = (unsigned long)ret_from_fork_user;
#if 0 /* 写完之后发现貌似不用 */
/* 准备用户栈数据 */
return do_fork(®s, 0);
}
-extern void ret_from_fork();
+extern void ret_from_fork_user();
+extern void ret_from_fork_krnl();
+
int do_fork(pt_regs_t *regs, unsigned long flags)
{
task_union *tsk;
tsk = alloc_task_union();
+ printk("another task %08x flags %08x\n", tsk, flags);
if(tsk == NULL)
panic("can not malloc PCB");
*child_regs = *regs;
child_regs->eax = 0;
+ child_regs->eflags |= 0x200; //enable IF
+
- regs->eax = 0x00;
tsk->esp0 = TASK_SIZE + (unsigned long) tsk;
tsk->esp = (unsigned long) child_regs;
- tsk->eip = (unsigned long) ret_from_fork;
-
- tsk->state = TASK_RUNNING;
-
-
- INIT_LIST_HEAD(&tsk->list);
- list_add(&tsk->list, &root_task.list);
+ tsk->eip = (unsigned long) ret_from_fork_user;
- return (int)tsk->pid;
-}
-
-#if 0
- init_tsk_cr3(tsk);
-
- int i, j;
- u32 *p_pd = (u32 *) current->cr3; // parent's page dir
- u32 p_pde; // parent's page dir entry
- u32 *p_pt = NULL;
- u32 *c_pd = (u32 *) tsk->cr3;
- u32 c_pde;
- u32 *c_pt = NULL;
-
- p_pd = pa2va(p_pd);
- c_pd = pa2va(c_pd);
-
- for(i = (KRNLADDR>>22); i>=0; i--)
+ if(flags & FORK_KRNL)
{
- p_pde = p_pd[i] & 0xFFFFF000;
- if(p_pde == 0)
- continue;
-
- //printk("i:%d p_pde:%08x ", i, p_pde);
-
- // 分配页表
- c_pde = (u32) get_phys_pages(1);
-
- //printk("c_pde:%08x ", c_pde);
-
- c_pt = pa2va(c_pde);
- p_pt = pa2va(p_pde);
-
- //printk("c_pt:%08x p_pt:%08x\n", c_pt, p_pt);
-
- for(j=0; j<PAGE_ITEMS; j++)
- {
- p_pt[j] &= (~2UL);
- c_pt[j] = p_pt[j];
- /* 增加此页的共享计数 */
- pgmap[p_pt[j]>>PAGE_SHIFT].count++;
- }
-
- c_pd[i] = c_pde | 7;
+ printk("--dsfsdafdsaf----\n");
+ tsk->eip= (unsigned long) ret_from_fork_krnl;
}
+ printk("tsk %08x child_regs esp %08x esp0 %08x\n", tsk, tsk->esp, tsk->esp0);
- load_cr3(current);
-
- //pPtRegs regs = ((pPtRegs)(TASK_SIZE+(unsigned long) current))-1;
- //tsk->regs = *regs;
- //tsk->regs.eax = 0x00;
- //tsk->regs.eflags |= 0x200; //enable IF
- //TODO pPtRegs regs = ((pPtRegs)(TASK_SIZE+(unsigned long) tsk))-1;
- extern void ret_from_fork();
- regs->eax = 0x00;
- tsk->esp0 = TASK_SIZE + (unsigned long) tsk;
- tsk->esp = (unsigned long) regs;
- tsk->eip = (unsigned long) ret_from_fork;
-
+ tsk->state = TASK_RUNNING;
- //printk("FORK:%08x\n", tsk);
- tsk->state = TASK_RUNNING;
+ INIT_LIST_HEAD(&tsk->list);
+ list_add(&tsk->list, &root_task.list);
return (int)tsk->pid;
}
-#endif
void root_task_entry();
-void setup_kernel();
TSS tss;
System system;
char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE)));
-int KernelEntry()
+void init_task_entry()
{
- setup_kernel();
-
-#if 0
- char *root_task_user_space_stack = (char *) alloc_pages(0, 0);
-
- asm("movl $0x23,%%eax; \
- movw %%ax,%%ds; \
- movw %%ax,%%es; \
- movw %%ax,%%fs; \
- movw %%ax,%%gs; \
- pushl $0x23; \
- pushl %%ebx; \
- pushl $0x282; \
- pushl $0x1B; \
- leal root_task_entry,%%eax; \
- pushl %%eax; \
- iret;"::"b"(root_task_user_space_stack+PAGE_SIZE));
-#else
-#if 0
- asm("xorl %eax, %eax; \
- sti;\
- pushfl; \
- movw %cs, %ax; \
- pushl %eax;\
- leal root_task_entry,%eax; \
- pushl %eax; \
- iret;"::"b"(root_task.cr3 + TASK_SIZE));
-#endif
-#endif
-
- return 0; /* never come to here */
-}
-
-#if 0
-void root_task_entry()
-{
- pid_t pid;
- pid = fork();
- if(pid < 0)
- {
- printf("failed create child\n");
- while(1);
- }
- else if(pid == 0)
- {
- execv("/bin/sh", NULL);
- while(1);
- }
- else
+ printk("hahahha %s\n", __func__);
+ while(1)
{
- while(1)
- {
-
- }
+ printk("i ");
+ asm("sti;hlt;");
}
}
-#else
-void root_task_entry()
-{
- pt_regs_t regs;
- int pid = do_fork(regs, 0);
- printk("pid is %d\n", pid);
-
- if(pid > 0)
- {
- while(1)
- {
- asm("hlt;");
- sysc_test();
- //syscall0(SYSC_TEST);
- }
- }
- else if(pid == 0)
- {
+void root_task_entry()
+{
+#if 0
+ while(1) {
+ asm("sti;hlt;");
}
- else
- {
- printk("err\n");
- }
- //pid_t pid;
-/*
- int fd = open("/boot/grub/grub.conf", O_RDONLY);
- //int fd = open("/bin/hw", O_RDONLY);
- printf("FD: %d\n", fd);
- char buf[1024];
- int filesz, i;
- Stat stat;
- fstat(fd, &stat);
- filesz = stat.st_size;
- printf("file size: %d bytes\n", filesz);
- read(fd, buf, filesz);
- for(i=0; i<filesz; i++)
- printf("%c", buf[i]);
- execv("/bin/hw", NULL);
- while(0)
- {
- printf("r");
- int d=100000; while(d--);
- }
-*/
-
-
-//#define SHOW
-
- pid = fork();
-#ifdef SHOW
- printf("PID: %d\n", pid);
-#endif
- if(pid < 0)
- {
- printf("failed create child\n");
- while(1);
- }
- else if(pid == 0)
- {
- pid_t ccid;
- ccid = fork();
-
-#ifdef SHOW
- printf("CCID: %d\n", ccid);
#endif
+ pt_regs_t regs;
+ memset((void*)®s, 0, sizeof(regs));
+ regs.edx = (unsigned long) init_task_entry;
+ cli();
+ int pid = do_fork(®s, FORK_KRNL);
+ sti();
- if(ccid < 0)
- {
- printf("child faild to create child\n");
- while(1);
- }
- else if(ccid == 0)
- {
- execv("/bin/sh", NULL);
- while(1)
- {
-#ifdef SHOW
- printf("a");
-#endif
- delay(100);
- }
- }
- else
- {
- while(1)
- {
-#ifdef SHOW
- printf("b");
-#endif
- delay(200);
- }
- }
- }
- else
+ printk("pid is %d\n", pid);
+ while(1)
{
- pid_t pcid;
- pcid = fork();
-
-#ifdef SHOW
- printf("PCID: %d\n", pcid);
-#endif
- if(pcid < 0)
- {
- printf("parent faild to create child\n");
- while(1);
- }
- else if(pcid == 0)
- {
- while(1)
- {
-#ifdef SHOW
- printf("c");
-#endif
- delay(300);
- }
- }
- else
- {
- while(1)
- {
-#if 0
- int k;
- extern void ParseKbdInput(int k);
- k = read_kbd();
- ParseKbdInput(k);
-#else
-#ifdef SHOW
- printf("d");
-#endif
- delay(400);
-#endif
- }
- }
+ printk("r ");
+ asm("sti;hlt;");
+ //sysc_test();
+ //syscall0(SYSC_TEST);
}
}
-#endif
.extern irq_handler
.extern schedule
_irq_handler:
- cli #FOR TEST ONLY
SAVE_REGS
movw %ss,%ax
RESTORE_REGS
addl $4,%esp /* 跳过中断号. */
-
iret
else
p = &root_task;
- if(p == &root_task)
- return ;
-
+ asm("nop;nop;nop;nop;");
+ printk("=");
task_union *prev, *next;
prev = current;
next = p;
+ printk("%08x ", next);
context_switch(prev, next);
#if 0
.code32
.text
.global syscall_entry
-.global ret_from_fork
+.global ret_from_fork_user
+.global ret_from_fork_krnl
.global sysexit
-#define EAX 32
+#define EAX 28
+#define EDX 20
syscall_entry:
movl (%esp),%esp
bad_sysc_nr:
call sysc_bad_syscnr
- jmp sysc_exit
+ jmp syscall_exit
-ret_from_fork:
+ret_from_fork_user:
xorl %eax, %eax
- jmp sysc_exit
+ jmp syscall_exit
+
+ret_from_fork_krnl:
+ movl EDX(%esp), %edx
+ call *%edx
+
+
# this routine should be load align by PAGE_SIZE
#if 1
void do_no_page(void *addr)
{
- //printk("%s ", __FUNCTION__);
- u32 *pde = (u32*)pa2va(current->cr3);
+ u32 *pde = (u32 *)current->cr3;
u32 *pte;
- void *page = get_phys_pages(1);
+ void *page = (void *)alloc_one_page(0);
if(page == NULL)
panic("failed alloc page");
if((pde[npde] & 0xFFFFF000)== 0)
{
- //printk("*a*\n");
- pte = (u32 *) pa2va(get_phys_pages(1));
+ pte = (u32 *) alloc_one_page(0);
memset((void*)pte, 0, PAGE_SIZE);
if(pte == NULL)
panic("failed alloc pte");
}
else
{
- //printk("*b* : %08x\n", page);
- //printk("*b* : %08x %08x\n", pde[npde], page);
pte = (u32*)(pde[npde] & 0xFFFFF000);
pte = pa2va(pte);
pte[npte] = (u32) page | 7;
}
load_cr3(current);
-#if 0
- u32 *pde = (u32*)pa2va(current->cr3);
- u32 *pte;
- void *page = (void*)va2pa(kmalloc_old(PAGE_SIZE));
- if(page == NULL)
- panic("failed alloc page");
-
- int npde = ((u32)addr)>>22;
- int npte = (((u32)addr)>>12) & 0x3FF;
-
- if(pde[npde] == 0)
- {
- printk("*a*");
- pte = (u32 *) kmalloc_old(PAGE_SIZE);
- memset((void*)pte, 0, PAGE_SIZE);
- if(pte == NULL)
- panic("failed alloc pte");
-
- pte[npte] = (u32) page | 7;
- pde[npde] = va2pa(pte) | 7;
- }
- else
- {
- printk("*b*");
- pte = pde[npde] & 0xFFFFF000;
- pte = pa2va(pte);
- pte[npte] = (u32) page | 7;
- }
-#endif
}
set_tss();
setup_sysc();
- setup_pci();
+ //setup_pci();
setup_irqs();