task_union root_task __attribute__((__aligned__(PAGE_SIZE)));
-pid_t get_next_pid()
+pid_t get_next_pid()
{
static pid_t g_pid = ROOT_TSK_PID;
- return g_pid++;
+
+ pid_t pid = g_pid;
+
+ g_pid += 123;
+
+ return pid;
}
-inline void load_cr3(task_union * tsk)
+inline void load_cr3(task_union * tsk)
{
- //printk("tsk %08x cr3: %08x\n",tsk, tsk->cr3);
- // asm("movl %%eax,%%cr3;"::"a"(tsk->cr3));
- //int j=10000; while(j--);
LOAD_CR3(tsk->cr3);
}
-void init_tsk_cr3(task_union * tsk)
+void init_tsk_cr3(task_union * tsk)
{
tsk->cr3 = (unsigned long) pa2va(get_phys_pages(1));
tsk->cr3 = va2pa(tsk->cr3);
}
-extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE)));
-void init_root_tsk()
+extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE)));
+void init_root_tsk()
{
int i;
root_task.cr3 = (unsigned long)init_pgd;
printk("init_root_task tss.esp0 %08x\n", tss.esp0);
-
- //init_tsk_cr3(root_task);
- //load_cr3(root_task);
-
- //current = &root_task;
-/*
- // 栈
- void *stack = kmalloc_old(PAGE_SIZE);
- if(stack == NULL)
- panic("stack");
- stack = va2pa(stack);
-
- printk("Stack : %08x\n", stack);
-
- u32 *pde = pa2va(current->cr3);
- u32 *pte = (u32 *)kmalloc_old(PAGE_SIZE);
- if(pte == NULL)
- panic("root task stack pte");
- pte[1023] = stack + 7;
- printk("pte: %08x\n", pte);
- pde[(KRNLADDR>>22)-1] = va2pa(pte) + 7;
-
-
- printk("CR3:%08x\n", current->cr3);
- asm("movl %%eax,%%cr3;"::"a"(current->cr3));
-*/
}
kmem_cache_t *task_union_cache;
-void setup_tasks()
+void setup_tasks()
{
init_root_tsk();
task_union_cache = kmem_cache_create("task_union", sizeof(task_union), PAGE_SIZE);
if(0 == task_union_cache)
panic("setup tasks failed. out of memory");
-
-#if 0
- add_task(test_taskB);
- add_task(test_taskA);
-#endif
}
task_union *alloc_task_union()
inline task_union * get_next_tsk()
{
-#if 0
- static unsigned int inx = 0;
- unsigned int i = 0;
- task_union *tsk = root_task;
-
- for(i=0; i<TASK_CNT; ++i)
- {
- inx = (inx + i) % TASK_CNT;
-
- task_union *p = root_task + inx;
-
- if(tsk->state == TASK_RUNNING)
- {
- tsk = p;
- break;
- }
- }
-
- return tsk;
-#endif
return 0;
}
inline void switch_to()
{
- //printk("current:%08x esp0:%08x\n", current, current->esp0);
load_cr3(current);
set_esp0(current);
}
inline void context_switch(task_union * prev, task_union * next)
{
- //task_union * last;
unsigned long eax, ebx, ecx, edx, esi, edi;
- //asm("xchg %bx, %bx");
asm volatile(
"pushfl;"
"pushl %%ebp;"
else
p = &root_task;
- asm("nop;nop;nop;nop;");
- printk("=");
task_union *prev, *next;
prev = current;
next = p;
- printk("%08x ", next);
- context_switch(prev, next);
-
-#if 0
- task_union * tsk, prev, next;
-
- cli(); // For Safe.
- tsk = current;
- do
- {
- tsk = get_next_tsk(tsk);
- }while(tsk->state == TASK_EXITING); /* 简单实现 */
-
- if(current == tsk)
- return;
-
- //tsk = current;
- //printk("tsk:%08x\t", tsk);
- //current = tsk;
- prev = current;
- current = next = tsk;
context_switch(prev, next);
-#endif
}
*/
#include <page.h>
-#include <bits.h>
#include <types.h>
#include <sched.h>
#include <assert.h>
#include <printk.h>
-#include <init.h>
#include <mm.h>
-
-
-#define get_page_from_list(pList) list_entry(pList, Page, list)
-#define add_page2list(page, order) \
- list_add(&page->list, &freeArea[order].freeList)
-
-FreeArea freeArea[MAX_OLD_ORDER];
-
-#if 1
void do_no_page(void *addr)
{
- u32 *pde = (u32 *)current->cr3;
- u32 *pte;
- void *page = (void *)alloc_one_page(0);
- if(page == NULL)
+ pde_t *pde = (pde_t *)current->cr3;
+ pte_t *pte;
+ unsigned long page = alloc_one_page(0);
+ if(page == 0)
panic("failed alloc page");
- int npde = ((u32)addr)>>22;
- int npte = (((u32)addr)>>12) & 0x3FF;
+ int npde = get_npd(addr);
+ int npte = get_npt(addr);
- if((pde[npde] & 0xFFFFF000)== 0)
+ if(PAGE_ALIGN(pde[npde]) == 0)
{
- pte = (u32 *) alloc_one_page(0);
+ pte = (pte_t*) alloc_one_page(0);
memset((void*)pte, 0, PAGE_SIZE);
if(pte == NULL)
panic("failed alloc pte");
- pte[npte] = (u32) page | 7;
- pde[npde] = va2pa(pte) | 7;
+ pte[npte] = (pte_t) page | PAGE_P | PAGE_WR | PAGE_US;
+ pde[npde] = va2pa(pte) | PAGE_P | PAGE_WR | PAGE_US;
}
else
{
- pte = (u32*)(pde[npde] & 0xFFFFF000);
+ pte = (pte_t*)(PAGE_ALIGN(pde[npde]));
pte = pa2va(pte);
- pte[npte] = (u32) page | 7;
+ pte[npte] = (u32) page | PAGE_P | PAGE_WR | PAGE_US;
}
+
load_cr3(current);
}
load_cr3(current);
}
-
-inline pPage __old_alloc_pages(unsigned int order, unsigned int alloc_order)
-{
- assert(0 <= order && order<MAX_OLD_ORDER);
- assert(!list_empty(&freeArea[order].freeList));
- assert(alloc_order <= order);
-
- pListHead pl = freeArea[order].freeList.next;
- list_del_init(pl);
- pPage page = get_page_from_list(pl);
-
- if(order == alloc_order)
- {
- page->order = alloc_order;
- page->count = 0;
- assert((page->mapNR%(1UL<<(order))) == 0);
- return page;
- }
-
- assert(order >= 1);
-
- // 把这个页分成两半挂到小一级的队列上
- add_page2list(page, order-1);
-#if 0
- if(page->mapNR == 0)
- {
- printk("[>1<%08x %d %d]\n", page, order, alloc_order);
- while(1);
- }
-#endif
- assert((page->mapNR%(1UL<<(order-1))) == 0);
- page += (1 << (order-1));
- assert((page->mapNR%(1UL<<(order-1))) == 0);
-#if 0
- if(page->mapNR == 0)
- {
- printk("[>2<%08x %d %d]\n", page, order, alloc_order);
- while(1);
- }
-#endif
- add_page2list(page, order-1);
-
- //int j=2000000;while(j--);
- return __old_alloc_pages(order-1, alloc_order);
-}
-
-pPage old_alloc_pages(unsigned int order)
-{
- if(order<0 || order>=MAX_OLD_ORDER)
- return NULL;
-
- int i;
- for(i=order; i<MAX_OLD_ORDER; i++)
- {
- if(list_empty(&freeArea[i].freeList))
- continue;
-
- return __old_alloc_pages(i, order);
- }
- return NULL;
-}
-
-
-pPage get_buddy_page(pPage page, unsigned int order)
-{
- assert(0<=order && order<MAX_OLD_ORDER);
- assert(page != NULL);
-
- //printk("mapnr: %d order:%d %d\n", page->mapNR, order, 1UL<<order);
- assert((page->mapNR%(1UL<<order)) == 0);
-
- int off = 1<<(order+0);
- int div = 1<<(order+1);
- int nr = page->mapNR;
-
- pListHead pos, tmp;
- pPage p;
-
- nr += ((nr+off)/div == nr/div) ? off : -off;
-
- list_for_each_safe(pos, tmp, &freeArea[order].freeList)
- {
- p = get_page_from_list(pos);
- if(p->mapNR == nr)
- {
- list_del_init(&p->list);
- p->order = order;
- return p;
- }
- }
-
-
- return NULL;
-}
-
-void old_free_pages(pPage page)
-{
- assert(page != NULL);
- unsigned int order = page->order;
- assert(0<=order && order<MAX_OLD_ORDER);
-
-
- // buddy page
- //printk("[%d %d]", page->mapNR, page->order);
- assert((page->mapNR % (1UL<<order)) == 0);
- pPage bpage = get_buddy_page(page, order);
- //printk("<%d %d>", bpage->mapNR, bpage->order);
-
-
- if(bpage == NULL || order == MAX_OLD_ORDER-1)
- {
- add_page2list(page, order);
- }
- else
- {
- //list_del_init(&bpage->list);
- page = (page->mapNR > bpage->mapNR) ? bpage : page;
- page->order += 1;
- assert((page->mapNR%(1UL<<page->order)) == 0);
-#if 0
- if((page->mapNR%(1UL<<page->order)) != 0)
- {
- printk("*%d %d*", page->mapNR, 1UL<<page->order);
- panic("bug");
- }
-#endif
- old_free_pages(page);
- }
-}
-
-#else
-
-inline pPage __old_alloc_pages(unsigned int order, unsigned int alloc_order)
-{
- assert(0 <= order && order<MAX_OLD_ORDER);
- assert(!list_empty(&freeArea[order].freeList));
- assert(alloc_order <= order);
-
- pListHead pl = freeArea[order].freeList.next;
- list_del_init(pl);
- pPage pg = list_entry(pl, Page, list);
- change_bit(pg->mapNR>>(order+1), (unsigned long *)freeArea[order].map);
-
- if(order == alloc_order)
- {
- return pg;
- }
-
- // 把这个页分成两半挂到小一级的队列上
- assert(order >= 1);
- list_add(&pg->list, &freeArea[order-1].freeList);
- pg += (1 << (order-1));
- list_add(&pg->list, &freeArea[order-1].freeList);
-
- return __old_alloc_pages(order-1, alloc_order);
-}
-
-pPage old_alloc_pages(unsigned int order)
-{
- if(order >= MAX_OLD_ORDER)
- return NULL;
-
- int i;
- for(i=order; i<MAX_OLD_ORDER; i++)
- {
- if(list_empty(&freeArea[i].freeList))
- continue;
-
- return __old_alloc_pages(i, order);
- }
-
- return NULL;
-}
-
-
-void old_free_pages(pPage page, unsigned int order)
-{
- assert(0<=order && order<MAX_OLD_ORDER);
- assert(page != NULL);
-
-
-
- int nr = page->mapNR>>(order+1);
- //printk("#########%d %d\n",page->mapNR, variable_test_bit( nr, (unsigned long *)freeArea[order].map));
- if(order == MAX_OLD_ORDER -1
- || !variable_test_bit( nr, (unsigned long *)freeArea[order].map))
- {
- change_bit( nr, (unsigned long *)freeArea[order].map);
- list_add(&page->list, &freeArea[order].freeList);
- }
- else
- {
-
- pListHead pos, tmp;
-
- int offset = 1<<order;
- nr = page->mapNR;
- printk("------------%d %d ", offset, nr);
- nr += (((nr+offset)>>(order)) == (nr>>(order)))
- ? offset : -offset;
- printk("%d\n", nr);
- list_for_each_safe(pos, tmp, &freeArea[order].freeList)
- {
- printk("[%d] ",list_entry(pos, Page, list)->mapNR);
- if(list_entry(pos, Page, list)->mapNR == nr)
- break;
- }
-
- assert(pos != &freeArea[order].freeList);
- list_del_init(pos);
-
- nr = page->mapNR>>(order+2);
- change_bit( nr, (unsigned long *)freeArea[order+1].map);
- pPage bpage = get_page_from_list(pos);
- page = (page->mapNR > bpage->mapNR)? bpage: page;
- list_add(&page->list, &freeArea[order+1].freeList);
- }
-}
-#endif
-
-void disp_free_area()
-{
- int i;
- for(i=0; i<MAX_OLD_ORDER; i++)
- {
- pListHead pos,tmp;
- int count = 0;
-
- list_for_each_safe(pos, tmp, &freeArea[i].freeList)
- {
- count++;
- //printk("%d ", list_entry(pos, Page, list)->mapNR);
- //printk("%08x %08x %08x %08x\n",pos, pos->prev, tmp,
- // &(freeArea[i].freeList));
- }
- printk("[%d %d]",i, count);
- }
- printk("\n");
- int j=100000;
- //while(j--);
-}