void MB_MarkPage(MemoryBitmap *Bitmap, physical Page, qword Flag) { switch (Flag) { case MB_PAGE_OCCUPIED: { address l_PageAddr = (address) Page; l_PageAddr = PAGE_SHIFT(PAGE_ROUND_DOWN(l_PageAddr)); if (l_PageAddr / 32 < Bitmap->m_Length) Bitmap->m_Bitmap[l_PageAddr / 32] &= ~(1 << (l_PageAddr % 32)); } return; case MB_PAGE_FREE: { address l_PageAddr = (address) Page; qword t_Div32, t_Mod32; l_PageAddr = PAGE_SHIFT(PAGE_ROUND_DOWN(l_PageAddr)); t_Div32 = l_PageAddr / 32; t_Mod32 = l_PageAddr % 32; if (t_Div32 < Bitmap->m_Length) Bitmap->m_Bitmap[t_Div32] |= 1 << t_Mod32; if (t_Div32 < Bitmap->m_LastIndex) Bitmap->m_LastIndex = t_Div32; } return; default: return; } }
int sos_brk_move(sos_pcb *pcb, seL4_Word new_brk_vaddr) { int err; sos_region *region; // First we need to find the brk region. // This is guaranteed to be the region containing the heap bottom. err = sos_resolve_region(&(pcb->regions), SOS_PROCESS_HEAP_START, ®ion); if (err != SOS_REGION_SUCCESS) { // This really should not happen - the heap is missing! // Not much we can do, but it's possible the process can still do work. dprintf(5, "ERROR: PID %d has no heap region.\n", pcb->pid); return SOS_PAGE_TABLE_OOM; } // ... Then we try to resize it. seL4_Word start_page = PAGE_SHIFT(region->base); seL4_Word end_page = PAGE_SHIFT(new_brk_vaddr); // XXX Check math - off by 1? err = _resize_region(pcb, region, end_page - start_page); return err; }
/* Statically-sized initial heap, for now */ static int _create_heap(sos_pcb *pcb) { int err; err = sos_create_region(pcb, SOS_PROCESS_HEAP_START, /* Number of pages in the initial heap region */ PAGE_SHIFT(SOS_PROCESS_BRK - SOS_PROCESS_HEAP_START), SOS_PROCESS_HEAP_ATTRS); if (err) return err; // Allocate heap guard page err = _create_guard_page(pcb, SOS_PROCESS_HEAP_GUARD); return err; }
/* Statically-sized stack frame, for now */ static int _create_stack_frame(sos_pcb *pcb) { int err; err = sos_create_region(pcb, SOS_PROCESS_STACK_BOTTOM, /* Number of frames in the fixed-size stack */ PAGE_SHIFT(SOS_PROCESS_STACK_TOP - SOS_PROCESS_STACK_BOTTOM), SOS_PROCESS_STACK_ATTRS); if (err) return err; // Allocate stack guard page err = _create_guard_page(pcb, SOS_PROCESS_STACK_GUARD); return err; }
void MB_ReleasePages(MemoryBitmap *Bmp, linear BlockAddr, qword Pages) { if (Pages == 0) return; if (Pages == 1) { MB_MarkPage(Bmp, LINEAR_TO_PHYSICAL(BlockAddr), MB_PAGE_FREE); return; } address l_BlockAddr = (address) LINEAR_TO_PHYSICAL(BlockAddr); qword t_Div32, t_Mod32; l_BlockAddr = PAGE_SHIFT(PAGE_ROUND_DOWN(l_BlockAddr)); t_Div32 = l_BlockAddr / 32; t_Mod32 = l_BlockAddr % 32; if (t_Div32 < Bmp->m_LastIndex) Bmp->m_LastIndex = t_Div32; while (t_Div32 < Bmp->m_Length && Pages != 0) { Bmp->m_Bitmap[t_Div32] |= (1 << t_Mod32); ++t_Mod32; if (t_Mod32 == 32) { ++t_Div32; t_Mod32 = 0; } --Pages; } }