2 * Physical memory allocation
3 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
7 * This is free software. You are permitted to use,
8 * redistribute, and modify it as specified in the file "COPYING".
14 #include <geekos/ktypes.h>
15 #include <geekos/defs.h>
16 #include <geekos/list.h>
17 #include <geekos/paging.h>
24 #define PAGE_AVAIL 0x0000 /* page is on the freelist */
25 #define PAGE_KERN 0x0001 /* page used by kernel code or data */
26 #define PAGE_HW 0x0002 /* page used by hardware (e.g., ISA hole) */
27 #define PAGE_ALLOCATED 0x0004 /* page is allocated */
28 #define PAGE_UNUSED 0x0008 /* page is unused */
29 #define PAGE_HEAP 0x0010 /* page is in kernel heap */
30 #define PAGE_PAGEABLE 0x0020 /* page can be paged out */
31 #define PAGE_LOCKED 0x0040 /* page is taken should not be freed */
32 #define PAGE_VM 0x0080 /* page is used by the VM */
38 * List datatype for doubly-linked list of Pages.
40 DEFINE_LIST(Page_List, Page);
43 * Each page of physical memory has one of these structures
44 * associated with it, to do allocation and bookkeeping.
47 unsigned flags; /* Flags indicating state of page */
48 DEFINE_LINK(Page_List, Page); /* Link fields for Page_List */
50 ulong_t vaddr; /* User virtual address where page is mapped */
51 pte_t *entry; /* Page table entry referring to the page */
54 IMPLEMENT_LIST(Page_List, Page);
56 void Init_Mem(struct Boot_Info* bootInfo);
58 void* Alloc_Page(void);
59 void* Alloc_Pageable_Page(pte_t *entry, ulong_t vaddr);
60 void Free_Page(void* pageAddr);
63 * Determine if given address is a multiple of the page size.
65 static __inline__ bool Is_Page_Multiple(ulong_t addr)
67 return addr == (addr & ~(PAGE_MASK));
71 * Round given address up to a multiple of the page size
73 static __inline__ ulong_t Round_Up_To_Page(ulong_t addr)
75 if ((addr & PAGE_MASK) != 0) {
83 * Round given address down to a multiple of the page size
85 static __inline__ ulong_t Round_Down_To_Page(ulong_t addr)
87 return addr & (~PAGE_MASK);
91 * Get the index of the page in memory.
93 static __inline__ int Page_Index(ulong_t addr)
95 return (int) (addr >> PAGE_POWER);
100 * Get the Page struct associated with given address.
102 static __inline__ struct Page *Get_Page(ulong_t addr)
104 extern struct Page* g_pageList;
105 return &g_pageList[Page_Index(addr)];
106 //return g_pageList + (sizeof(struct Page) * (int)(addr >> PAGE_POWER));
110 * Get the physical address of the memory represented by given Page object.
112 static __inline__ ulong_t Get_Page_Address(struct Page *page)
114 extern struct Page* g_pageList;
115 ulong_t index = page - g_pageList;
116 return index << PAGE_POWER;
119 #endif /* GEEKOS_MEM_H */