2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
17 #define KERNEL_STACKS 0xF0000000
18 #define KERNEL_STACK_SIZE 0x00008000
19 #define KERNEL_STACKS_END 0xFD000000
20 #define WORKER_STACKS 0x00100000 // Thread0 Only!
21 #define WORKER_STACK_SIZE KERNEL_STACK_SIZE
22 #define WORKER_STACKS_END 0xB0000000
23 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
24 #define PAGE_TABLE_ADDR 0xFD000000
25 #define PAGE_DIR_ADDR 0xFD3F4000
26 #define PAGE_CR3_ADDR 0xFD3F4FD0
27 #define TMP_CR3_ADDR 0xFD3F4FD4 // Part of core instead of temp
28 #define TMP_DIR_ADDR 0xFD3F5000 // Same
29 #define TMP_TABLE_ADDR 0xFD400000
30 #define HW_MAP_ADDR 0xFD800000
31 #define HW_MAP_MAX 0xFEFF0000
32 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
33 #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages
34 #define NUM_TEMP_PAGES 16
36 #define PF_PRESENT 0x1
40 #define PF_PAGED 0x400
42 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
45 extern Uint32 gaInitPageDir[1024];
46 extern Uint32 gaInitPageTable[1024];
47 extern void Threads_SegFault(tVAddr Addr);
48 extern void Error_Backtrace(Uint eip, Uint ebp);
51 void MM_PreinitVirtual();
52 void MM_InstallVirtual();
53 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
54 void MM_DumpTables(tVAddr Start, tVAddr End);
55 tPAddr MM_DuplicatePage(tVAddr VAddr);
58 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
59 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
60 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
61 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
62 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
63 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
64 int gilTempMappings = 0;
65 int gilTempFractal = 0;
66 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
67 int giLastUsedWorker = 0;
71 * \fn void MM_PreinitVirtual()
72 * \brief Maps the fractal mappings
74 void MM_PreinitVirtual()
76 gaInitPageDir[ 0 ] = 0;
77 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
81 * \fn void MM_InstallVirtual()
82 * \brief Sets up the constant page mappings
84 void MM_InstallVirtual()
88 // --- Pre-Allocate kernel tables
89 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
91 if( gaPageDir[ i ] ) continue;
92 // Skip stack tables, they are process unique
93 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
98 gaPageDir[ i ] = MM_AllocPhys() | 3;
99 INVLPG( &gaPageTable[i*1024] );
100 memset( &gaPageTable[i*1024], 0, 0x1000 );
105 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
106 * \brief Called on a page fault
108 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
110 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
112 // -- Check for COW --
113 if( gaPageDir [Addr>>22] & PF_PRESENT
114 && gaPageTable[Addr>>12] & PF_PRESENT
115 && gaPageTable[Addr>>12] & PF_COW )
118 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
120 gaPageTable[Addr>>12] &= ~PF_COW;
121 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
125 paddr = MM_DuplicatePage( Addr );
126 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
127 gaPageTable[Addr>>12] &= PF_USER;
128 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
131 INVLPG( Addr & ~0xFFF );
136 // If it was a user, tell the thread handler
138 Warning("%s %s %s memory%s",
139 (ErrorCode&4?"User":"Kernel"),
140 (ErrorCode&2?"write to":"read from"),
141 (ErrorCode&1?"bad/locked":"non-present"),
142 (ErrorCode&16?" (Instruction Fetch)":"")
144 Warning("User Pagefault: Instruction at %p accessed %p", Regs->eip, Addr);
145 __asm__ __volatile__ ("sti"); // Restart IRQs
146 Threads_SegFault(Addr);
150 // -- Check Error Code --
152 Warning("Reserved Bits Trashed!");
155 Warning("%s %s %s memory%s",
156 (ErrorCode&4?"User":"Kernel"),
157 (ErrorCode&2?"write to":"read from"),
158 (ErrorCode&1?"bad/locked":"non-present"),
159 (ErrorCode&16?" (Instruction Fetch)":"")
163 Log("Code at %p accessed %p", Regs->eip, Addr);
164 // Print Stack Backtrace
165 Error_Backtrace(Regs->eip, Regs->ebp);
167 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
168 if( gaPageDir[Addr>>22] & PF_PRESENT )
169 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
171 //MM_DumpTables(0, -1);
173 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
177 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
178 * \brief Dumps the layout of the page tables
180 void MM_DumpTables(tVAddr Start, tVAddr End)
182 tVAddr rangeStart = 0;
186 const tPAddr MASK = ~0xF98;
188 Start >>= 12; End >>= 12;
191 Log("Directory Entries:");
192 for(page = Start >> 10;
193 page < (End >> 10)+1;
198 Log(" 0x%08x-0x%08x :: 0x%08x",
199 page<<22, ((page+1)<<22)-1,
200 gaPageDir[page]&~0xFFF
206 Log("Table Entries:");
207 for(page = Start, curPos = Start<<12;
209 curPos += 0x1000, page++)
211 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
212 || !(gaPageTable[page] & PF_PRESENT)
213 || (gaPageTable[page] & MASK) != expected)
216 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
217 rangeStart, curPos - 1,
218 gaPageTable[rangeStart>>12] & ~0xFFF,
219 (expected & ~0xFFF) - 1,
220 (expected & PF_PAGED ? "p" : "-"),
221 (expected & PF_COW ? "C" : "-"),
222 (expected & PF_USER ? "U" : "-"),
223 (expected & PF_WRITE ? "W" : "-")
227 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
228 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
230 expected = (gaPageTable[page] & MASK);
233 if(expected) expected += 0x1000;
237 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
238 rangeStart, curPos - 1,
239 gaPageTable[rangeStart>>12] & ~0xFFF,
240 (expected & ~0xFFF) - 1,
241 (expected & PF_PAGED ? "p" : "-"),
242 (expected & PF_COW ? "C" : "-"),
243 (expected & PF_USER ? "U" : "-"),
244 (expected & PF_WRITE ? "W" : "-")
251 * \fn tPAddr MM_Allocate(tVAddr VAddr)
253 tPAddr MM_Allocate(tVAddr VAddr)
256 // Check if the directory is mapped
257 if( gaPageDir[ VAddr >> 22 ] == 0 )
259 // Allocate directory
260 paddr = MM_AllocPhys();
262 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
266 gaPageDir[ VAddr >> 22 ] = paddr | 3;
268 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
270 INVLPG( &gaPageDir[ VAddr >> 22 ] );
271 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
273 // Check if the page is already allocated
274 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
275 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
276 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
280 paddr = MM_AllocPhys();
282 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
283 VAddr, __builtin_return_address(0));
287 gaPageTable[ VAddr >> 12 ] = paddr | 3;
289 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
290 // Invalidate Cache for address
291 INVLPG( VAddr & ~0xFFF );
297 * \fn void MM_Deallocate(tVAddr VAddr)
299 void MM_Deallocate(tVAddr VAddr)
301 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
302 Warning("MM_Deallocate - Directory not mapped");
306 if(gaPageTable[ VAddr >> 12 ] == 0) {
307 Warning("MM_Deallocate - Page is not allocated");
312 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
314 gaPageTable[ VAddr >> 12 ] = 0;
318 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
319 * \brief Checks if the passed address is accesable
321 tPAddr MM_GetPhysAddr(tVAddr Addr)
323 if( !(gaPageDir[Addr >> 22] & 1) )
325 if( !(gaPageTable[Addr >> 12] & 1) )
327 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
332 * \fn int MM_IsUser(tVAddr VAddr)
333 * \brief Checks if a page is user accessable
335 int MM_IsUser(tVAddr VAddr)
337 if( !(gaPageDir[VAddr >> 22] & 1) )
339 if( !(gaPageTable[VAddr >> 12] & 1) )
341 if( !(gaPageTable[VAddr >> 12] & PF_USER) )
347 * \fn void MM_SetCR3(tPAddr CR3)
348 * \brief Sets the current process space
350 void MM_SetCR3(tPAddr CR3)
352 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
356 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
357 * \brief Map a physical page to a virtual one
359 int MM_Map(tVAddr VAddr, tPAddr PAddr)
361 //ENTER("xVAddr xPAddr", VAddr, PAddr);
363 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
364 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
370 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
372 // Check if the directory is mapped
373 if( gaPageDir[ VAddr >> 22 ] == 0 )
375 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
378 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
380 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
381 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
383 // Check if the page is already allocated
384 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
385 Warning("MM_Map - Allocating to used address");
391 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
393 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
395 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
396 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
401 //LOG("INVLPG( 0x%x )", VAddr);
409 * \fn tVAddr MM_ClearUser()
410 * \brief Clear user's address space
412 tVAddr MM_ClearUser()
417 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
419 // Check if directory is not allocated
420 if( !(gaPageDir[i] & PF_PRESENT) ) {
426 for( j = 0; j < 1024; j ++ )
428 if( gaPageTable[i*1024+j] & 1 )
429 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
430 gaPageTable[i*1024+j] = 0;
433 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
435 INVLPG( &gaPageTable[i*1024] );
443 * \fn tPAddr MM_Clone()
444 * \brief Clone the current address space
451 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
454 LOCK( &gilTempFractal );
456 // Create Directory Table
457 *gTmpCR3 = MM_AllocPhys() | 3;
459 //LOG("Allocated Directory (%x)", *gTmpCR3);
460 memsetd( gaTmpDir, 0, 1024 );
465 // Check if table is allocated
466 if( !(gaPageDir[i] & PF_PRESENT) ) {
472 // Allocate new table
473 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
474 INVLPG( &gaTmpTable[page] );
476 for( j = 0; j < 1024; j ++, page++ )
478 if( !(gaPageTable[page] & PF_PRESENT) ) {
479 gaTmpTable[page] = 0;
484 MM_RefPhys( gaPageTable[page] & ~0xFFF );
486 if(gaPageTable[page] & PF_WRITE) {
487 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
488 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
489 INVLPG( page << 12 );
492 gaTmpTable[page] = gaPageTable[page];
496 // Map in kernel tables (and make fractal mapping)
497 for( i = 768; i < 1024; i ++ )
500 if( i == (PAGE_TABLE_ADDR >> 22) ) {
501 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
505 if( gaPageDir[i] == 0 ) {
510 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
511 MM_RefPhys( gaPageDir[i] & ~0xFFF );
512 gaTmpDir[i] = gaPageDir[i];
515 // Allocate kernel stack
516 for(i = KERNEL_STACKS >> 22;
517 i < KERNEL_STACKS_END >> 22;
520 // Check if directory is allocated
521 if( (gaPageDir[i] & 1) == 0 ) {
526 // We don't care about other kernel stacks, just the current one
527 if( i != kStackBase >> 22 ) {
528 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
534 gaTmpDir[i] = MM_AllocPhys() | 3;
535 INVLPG( &gaTmpTable[i*1024] );
536 for( j = 0; j < 1024; j ++ )
538 // Is the page allocated? If not, skip
539 if( !(gaPageTable[i*1024+j] & 1) ) {
540 gaTmpTable[i*1024+j] = 0;
544 // We don't care about other kernel stacks
545 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
546 gaTmpTable[i*1024+j] = 0;
551 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
553 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
555 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
556 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
557 MM_FreeTemp( (Uint)tmp );
561 ret = *gTmpCR3 & ~0xFFF;
562 RELEASE( &gilTempFractal );
569 * \fn tVAddr MM_NewKStack()
570 * \brief Create a new kernel stack
572 tVAddr MM_NewKStack()
574 tVAddr base = KERNEL_STACKS;
576 for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
578 if(MM_GetPhysAddr(base) != 0) continue;
579 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
582 return base+KERNEL_STACK_SIZE;
584 Warning("MM_NewKStack - No address space left\n");
589 * \fn tVAddr MM_NewWorkerStack()
590 * \brief Creates a new worker stack
592 tVAddr MM_NewWorkerStack()
599 tPAddr pages[WORKER_STACK_SIZE>>12];
601 // Get the old ESP and EBP
602 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
603 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
605 // Find a free worker stack address
606 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
609 if( gWorkerStacks[base/32] == -1 ) {
610 base += 31; base &= ~31;
611 base --; // Counteracted by the base++
615 if( gWorkerStacks[base/32] & (1 << base) ) {
620 if(base >= NUM_WORKER_STACKS) {
621 Warning("Uh-oh! Out of worker stacks");
626 gWorkerStacks[base/32] |= (1 << base);
627 // Make life easier for later calls
628 giLastUsedWorker = base;
630 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
631 //Log(" MM_NewWorkerStack: base = 0x%x", base);
633 // Acquire the lock for the temp fractal mappings
634 LOCK(&gilTempFractal);
636 // Set the temp fractals to TID0's address space
637 *gTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
638 //Log(" MM_NewWorkerStack: *gTmpCR3 = 0x%x", *gTmpCR3);
642 // Check if the directory is mapped (we are assuming that the stacks
643 // will fit neatly in a directory)
644 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
645 if(gaTmpDir[ base >> 22 ] == 0) {
646 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
647 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
651 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
653 pages[ addr >> 12 ] = MM_AllocPhys();
654 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
657 // Release the temp mapping lock
658 RELEASE(&gilTempFractal);
660 // Copy the old stack
661 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
662 esp = oldstack - esp; // ESP as an offset in the stack
664 // Make `base` be the top of the stack
665 base += WORKER_STACK_SIZE;
667 i = (WORKER_STACK_SIZE>>12) - 1;
668 // Copy the contents of the old stack to the new one, altering the addresses
669 // `addr` is refering to bytes from the stack base (mem downwards)
670 for(addr = 0; addr < esp; addr += 0x1000)
672 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
673 tmpPage = (void*)MM_MapTemp( pages[i] );
675 for(j = 0; j < 1024; j++)
677 // Possible Stack address?
678 if(oldstack-esp < stack[j] && stack[j] < oldstack)
679 tmpPage[j] = base - (oldstack - stack[j]);
680 else // Seems not, best leave it alone
681 tmpPage[j] = stack[j];
683 MM_FreeTemp((tVAddr)tmpPage);
687 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
692 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
693 * \brief Sets the flags on a page
695 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
698 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
699 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
701 ent = &gaPageTable[VAddr >> 12];
704 if( Mask & MM_PFLAG_RO )
706 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
707 else *ent |= PF_WRITE;
711 if( Mask & MM_PFLAG_KERNEL )
713 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
714 else *ent |= PF_USER;
718 if( Mask & MM_PFLAG_COW )
720 if( Flags & MM_PFLAG_COW ) {
732 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
733 * \brief Duplicates a virtual page to a physical one
735 tPAddr MM_DuplicatePage(tVAddr VAddr)
742 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
743 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
749 ret = MM_AllocPhys();
751 // Write-lock the page (to keep data constistent), saving its R/W state
752 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
753 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
757 temp = MM_MapTemp(ret);
758 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
761 // Restore Writeable status
762 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
769 * \fn Uint MM_MapTemp(tPAddr PAddr)
770 * \brief Create a temporary memory mapping
771 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
773 tVAddr MM_MapTemp(tPAddr PAddr)
777 //ENTER("XPAddr", PAddr);
781 //LOG("gilTempMappings = %i", gilTempMappings);
785 LOCK( &gilTempMappings );
787 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
789 // Check if page used
790 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
792 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
793 INVLPG( TEMP_MAP_ADDR + (i << 12) );
794 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
795 RELEASE( &gilTempMappings );
796 return TEMP_MAP_ADDR + (i << 12);
798 RELEASE( &gilTempMappings );
804 * \fn void MM_FreeTemp(tVAddr PAddr)
805 * \brief Free's a temp mapping
807 void MM_FreeTemp(tVAddr VAddr)
810 //ENTER("xVAddr", VAddr);
812 if(i >= (TEMP_MAP_ADDR >> 12))
813 gaPageTable[ i ] = 0;
819 * \fn tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number)
820 * \brief Allocates a contigous number of pages
822 tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number)
829 for( i = 0; i < NUM_HW_PAGES; i ++ )
831 // Check if addr used
832 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
835 // Check possible region
836 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
838 // If there is an allocated page in the region we are testing, break
839 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
845 for( j = 0; j < Number; j++ ) {
846 MM_RefPhys( PAddr + (j<<12) );
847 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
849 return HW_MAP_ADDR + (i<<12);
852 // If we don't find any, return NULL
857 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
858 * \brief Allocates DMA physical memory
859 * \param Pages Number of pages required
860 * \param MaxBits Maximum number of bits the physical address can have
861 * \param PhysAddr Pointer to the location to place the physical address allocated
862 * \return Virtual address allocate
864 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
866 tPAddr maxCheck = (1 << MaxBits);
870 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
873 if(MaxBits < 12 || !PhysAddr) {
879 if(MaxBits >= PHYS_BITS) maxCheck = -1;
882 if(Pages == 1 && MaxBits >= PHYS_BITS)
884 phys = MM_AllocPhys();
886 ret = MM_MapHWPage(phys, 1);
897 phys = MM_AllocPhysRange(Pages);
898 // - Was it allocated?
903 // - Check if the memory is OK
904 if(phys + (Pages-1)*0x1000 > maxCheck)
906 // Deallocate and return 0
907 for(;Pages--;phys+=0x1000)
913 // Allocated successfully, now map
914 ret = MM_MapHWPage(phys, Pages);
916 // If it didn't map, free then return 0
917 for(;Pages--;phys+=0x1000)
929 * \fn void MM_UnmapHWPage(tVAddr VAddr, Uint Number)
930 * \brief Unmap a hardware page
932 void MM_UnmapHWPage(tVAddr VAddr, Uint Number)
936 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
940 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
942 for( j = 0; j < Number; j++ )
944 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
945 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
948 RELEASE( &gilTempMappings );
952 EXPORT(MM_GetPhysAddr);
955 EXPORT(MM_MapHWPage);
957 EXPORT(MM_UnmapHWPage);