# enable PG
movl %cr0,%eax
- orl $0x80000000,%eax
+ orl $0x80010000,%eax
movl %eax,%cr0
#ifndef _PAGE_H
#define _PAGE_H
-
-
#define PAGE_P 0x1
#define PAGE_WR 0x2
#define PAGE_US 0x4
void *page2va(page_t *page);
page_t *va2page(unsigned long addr);
-#define pa2page(addr) va2page((unsigned long)pa2va(addr))
+#define pa2page(addr) va2page((unsigned long)pa2va(PAGE_ALIGN(addr)))
static inline page_t *get_head_page(page_t *page) { return page->head_page; }
free_virt_pages((void*)va2pa(p));
}
-#define panic(msg) do { \
- asm("cli;"); \
- printk("PANIC:\"%s\" file:%s function:%s line:%d\n", \
- msg, __FILE__, __FUNCTION__, __LINE__); \
- while(1); \
+#define panic(msg, ...) do { \
+ asm("cli;"); \
+ printk("PANIC:\" # msg # \" file:%s function:%s line:%d\n", \
+ ##__VA_ARGS__, __FILE__, __FUNCTION__, __LINE__); \
+ while(1); \
} while(0);
extern char etext, edata, end;
/* 先简要实现 */
current->state = TASK_EXITING;
+ task_union *t = current;
schedule();
if(tsk == NULL)
panic("can not malloc PCB");
+ memcpy(tsk, current, sizeof(task_union));
+
{
tsk->cr3 = (unsigned long) alloc_one_page(0);
+ printl(MPL_TEST+1, "cr3 %08x", tsk->cr3);
if(tsk->cr3 == 0)
panic("failed init tsk cr3");
{
unsigned long spde = (unsigned long) pde_src[i];
unsigned long dpde = 0;
+
+ if(i>=768)
+ {
+ pde_dst[i] = pde_src[i];
+ continue;
+ }
+
if(spde != 0)
dpde = PAGE_FLAGS(spde) | (unsigned long) va2pa(alloc_one_page(0));
pde_dst[i] = dpde;
-
pte_t *pte_src = pa2va(PAGE_ALIGN(spde));
pte_t *pte_dst = pa2va(PAGE_ALIGN(dpde));
for(j=0; j< PAGE_PTE_CNT; ++j)
}
}
- memcpy(tsk, current, sizeof(task_union));
-
tsk->pid = get_next_pid();
tsk->ppid = current->pid;
int debug_wait_queue_get();
+#if 0
void ring3()
{
+ int i = 0;
while(1)
{
+ i++;
+// printk("fuck\n");
systest();
}
}
+#else
+void ring3();
+#endif
static char user_task_stack[PAGE_SIZE] __attribute__ ((__aligned__(PAGE_SIZE)));
void user_task_entry()
{
printk("user_task_entry\n");
- 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 ring3,%%eax; \
- pushl %%eax; \
+ //while(1);
+#if 1
+ asm("cli;");
+ asm("movl $0x23,%%eax; \
+ movw %%ax,%%ds; \
+ movw %%ax,%%es; \
+ movw %%ax,%%fs; \
+ movw %%ax,%%gs; \
+ pushl $0x23; \
+ pushl %%ebx; \
+ pushl $0x202; \
+ pushl $0x1B; \
+ leal ring3,%%eax; \
+ pushl %%eax; \
iret;"::"b"(user_task_stack+PAGE_SIZE));
+#else
+ asm("xorl %eax,%eax; \
+ sti; \
+ pushfl; \
+ movw %cs,%ax; \
+ pushl %eax; \
+ leal ring3,%eax; \
+ pushl %eax; \
+ iret;");
+#endif
}
void init_task_entry()
while(1)
{
- printl(MPL_TASK_1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++);
- //int v = debug_wait_queue_get();
- //printk("task:%d wait queue get %d\n", id, v);
+ printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++);
+ int v = debug_wait_queue_get();
+ printk("task:%d wait queue get %d\n", id, v);
}
}
void root_task_entry()
{
- //kernel_task(init_task_entry);
+
+ kernel_task(init_task_entry);
+ kernel_task(init_task_entry);
kernel_task(user_task_entry);
+
int cnt = 0;
while(1)
{
a = 0;
+ //printd("do page fault errcode %x addr %08x\n", errcode, addr);
+
+ if(errcode & PAGE_US)
+ {
+ panic("user program try to access a page and cause a protection fault. addr %08x", addr);
+ }
+
if((errcode & PAGE_P) == 0)
{
extern void do_no_page(void *);
unsigned long esp;
asm("movl %%esp, %%eax":"=a"(esp));
- printl(MPL_PREEMPT, "current %08x preempt %d esp %08x", current, current->preempt_cnt, esp);
+ printl(MPL_PREEMPT, "current %08x cr3 %08x preempt %d esp %08x", current, current->cr3, current->preempt_cnt, esp);
p->chip->ack(irq);
sti();
LOAD_CR3(tsk->cr3);
}
-void init_tsk_cr3(task_union * tsk)
-{
- tsk->cr3 = (unsigned long) pa2va(get_phys_pages(1));
-
- if(tsk->cr3 == 0)
- panic("failed init tsk cr3");
-
- memset((void *)tsk->cr3, 0, PAGE_SIZE);
- memcpy((void *)tsk->cr3, (void*)system.page_dir, PAGE_SIZE);
- tsk->cr3 = va2pa(tsk->cr3);
-}
-
extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE)));
void init_root_tsk()
{
task_union *prev = current;
task_union *next = sel;
- if(prev != sel)
+ if(prev != next)
+ {
+ printd("select %08x\n", next);
context_switch(prev, next);
+ }
}
void debug_sched()
popl %edx;
popl %ecx;
ret
+
+.global ring3
+ring3:
+ nop;
+ nop;
+
+ movl $(ring3_stack+100), %esp
+
+ movl $11, %eax;
+
+ pushl $1f;
+ pushl %ecx;
+ pushl %edx;
+ pushl %ebp;
+ movl %esp,%ebp;
+ sysenter;
+ 1:
+
+ nop;
+ nop;
+
+ jmp ring3
+
+ring3_stack:
+ .byte 200
int sysc_test()
{
- static unsigned int cnt;
+ static unsigned int cnt=0;
printl(MPL_TEST, "sysc_test %u", cnt++);
}
int npde = get_npd(addr);
int npte = get_npt(addr);
- unsigned long *pd = (u32 *)pa2va(current->cr3);
+ //unsigned long *pd = (u32 *)pa2va(current->cr3);
+ //unsigned long *pd = (u32 *)va2pa(current->cr3);
+ unsigned long *pd = (u32 *)(current->cr3);
unsigned long *pt = NULL;
pt = pa2va(PAGE_ALIGN(pd[npde]));
if(0 == dst)
panic("out of memory");
+ dst = va2pa(dst);
+
pt[npte] = dst | flags;
dst = (unsigned long)pa2va(PAGE_ALIGN(dst));
}
else
{
+ //pd[npde] |= PAGE_WR;
pt[npte] |= PAGE_WR;
+ //pd[npde] |= PAGE_US;
+ //pt[npte] |= PAGE_US;
}
load_cr3(current);