Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
kmem.h File Reference
#include <sys/queue.h>
#include <sys/spinlock.h>
Include dependency graph for kmem.h:

Go to the source code of this file.

Data Structures

struct  Page
 
struct  SlabElement
 
struct  Slab
 

Macros

#define SLAB_NAMELEN   32
 
#define DECLARE_SLAB(_type)
 
#define DEFINE_SLAB(_type, _pool)
 

Typedefs

typedef struct Page Page
 
typedef struct XMem XMem
 
typedef struct SlabElement SlabElement
 
typedef struct Slab Slab
 

Functions

void PAlloc_Init ()
 
void PAlloc_AddRegion (uintptr_t start, uintptr_t len)
 
void * PAlloc_AllocPage ()
 
void PAlloc_Retain (void *pg)
 
void PAlloc_Release (void *pg)
 
void XMem_Init ()
 
XMemXMem_New ()
 
void XMem_Destroy (XMem *xmem)
 
uintptr_t XMem_GetBase (XMem *xmem)
 
uintptr_t XMem_GetLength (XMem *xmem)
 
bool XMem_Allocate (XMem *xmem, uintptr_t length)
 
void Slab_Init (Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
 
void * Slab_Alloc (Slab *slab) __attribute__((malloc))
 
void Slab_Free (Slab *slab, void *obj)
 

Data Structure Documentation

◆ Page

struct Page

Definition at line 8 of file kmem.h.

Collaboration diagram for Page:
[legend]
Data Fields
uint32_t _unused
uint32_t flags
uint32_t pincount
uint32_t refcount

Macro Definition Documentation

◆ DECLARE_SLAB

#define DECLARE_SLAB (   _type)
Value:
_type *_type##_Alloc(); \
void _type##_Free(_type *obj);

Definition at line 65 of file kmem.h.

◆ DEFINE_SLAB

#define DEFINE_SLAB (   _type,
  _pool 
)
Value:
_type *_type##_Alloc() { \
return (_type *)Slab_Alloc(_pool); \
} \
void _type##_Free(_type *obj) { \
Slab_Free(_pool, obj); \
}
void * Slab_Alloc(Slab *slab) __attribute__((malloc))
Definition: slab.c:105

Definition at line 69 of file kmem.h.

◆ SLAB_NAMELEN

#define SLAB_NAMELEN   32

Definition at line 40 of file kmem.h.

Typedef Documentation

◆ Page

typedef struct Page Page

◆ Slab

typedef struct Slab Slab

◆ SlabElement

typedef struct SlabElement SlabElement

◆ XMem

typedef struct XMem XMem

Definition at line 27 of file kmem.h.

Function Documentation

◆ PAlloc_AddRegion()

void PAlloc_AddRegion ( uintptr_t  start,
uintptr_t  len 
)

PAlloc_AddRegion –

Add a physical memory region to the page allocator.

Definition at line 92 of file palloc.c.

93{
94 uintptr_t i;
95 FreePage *pg;
96
97 if ((start % PGSIZE) != 0)
98 Panic("Region start is not page aligned!");
99 if ((len % PGSIZE) != 0)
100 Panic("Region length is not page aligned!");
101
102 /*
103 * PageInfo table isn't initialized on the first call to this function. We
104 * must allocate a temporary table that will be copied into the XMem region
105 * inside PAlloc_LateInit.
106 *
107 * Note that the PageInfo table is invalid for regions that are not added
108 * to the free list such as MMIO regions.
109 */
110 if (pageInfoTable == NULL) {
111 // Physical Address Offsets
113 uintptr_t end = base + len;
114
115 pageInfoLength = ROUNDUP(end / PGSIZE * sizeof(PageInfo), PGSIZE);
116 pageInfoTable = (PageInfo *)start;
117
118 start += pageInfoLength;
120
121 for (i = 0; i < (base / PGSIZE); i++) {
122 pageInfoTable[i].refCount = 1;
123 }
124 for (i = (base / PGSIZE); i < (end / PGSIZE); i++) {
125 pageInfoTable[i].refCount = 0;
126 }
127 for (i = 0; i < (pageInfoLength / PGSIZE); i++) {
128 pageInfoTable[i + (base / PGSIZE)].refCount = 1;
129 }
130 } else {
131 /*
132 * Only the first call to AddRegion should occur before the XMem region
133 * is initialized.
134 */
135
137
139 uintptr_t end = base + len;
140
141 uintptr_t newLength = ROUNDUP(end / PGSIZE * sizeof(PageInfo), PGSIZE);
142
143 if (!XMem_Allocate(pageInfoXMem, newLength))
144 Panic("Cannot allocate XMem region!");
145
146 // Initialize new pages
147 for (i = (base / PGSIZE); i < (end / PGSIZE); i++) {
148 pageInfoTable[i].refCount = 0;
149 }
150 }
151
153 for (i = 0; i < len; i += PGSIZE)
154 {
155 pg = (void *)(start + i);
157
158 totalPages++;
159 freePages++;
160
161 LIST_INSERT_HEAD(&freeList, pg, entries);
162 }
164}
#define ASSERT(_x)
Definition: kassert.h:8
#define ROUNDUP(_x, _n)
Definition: malloc.c:32
#define PGSIZE
Definition: malloc.c:21
uint64_t len
Definition: multiboot.h:2
PageInfo * pageInfoTable
Definition: palloc.c:43
#define FREEPAGE_MAGIC_FREE
Definition: palloc.c:23
uint64_t refCount
Definition: palloc.c:39
uint64_t magic
Definition: palloc.c:33
uint64_t pageInfoLength
Definition: palloc.c:44
uint64_t totalPages
Definition: palloc.c:28
XMem * pageInfoXMem
Definition: palloc.c:42
Spinlock pallocLock
Definition: palloc.c:27
uint64_t freePages
Definition: palloc.c:29
#define DMVA2PA(dmva)
Definition: pmap.h:47
#define LIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:451
static uint16_t base
Definition: sercons.c:37
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
#define NULL
Definition: stddef.h:6
uint64_t uintptr_t
Definition: types.h:16
void Panic(const char *str)
Definition: vgacons.c:164
bool XMem_Allocate(XMem *xmem, uintptr_t length)
Definition: xmem.c:92

◆ PAlloc_AllocPage()

void * PAlloc_AllocPage ( )

PAlloc_AllocPage –

Allocate a physical page and return the page's address in the Kernel's ident mapped memory region.

Return values
NULLif no memory is available.
Returns
Newly allocated physical page.

Definition at line 188 of file palloc.c.

189{
190 PageInfo *info;
191 FreePage *pg;
192
194 pg = LIST_FIRST(&freeList);
195 ASSERT(pg != NULL);
196 LIST_REMOVE(pg, entries);
197
199
200 info = PAllocGetInfo(pg);
201 ASSERT(info != NULL);
202 ASSERT(info->refCount == 0);
203 info->refCount++;
204
206
207 freePages--;
209
210 memset(pg, 0, PGSIZE);
211
212 return (void *)pg;
213}
static PageInfo * PAllocGetInfo(void *pg)
Definition: palloc.c:172
#define FREEPAGE_MAGIC_INUSE
Definition: palloc.c:25
#define LIST_REMOVE(elm, field)
Definition: queue.h:465
#define LIST_FIRST(head)
Definition: queue.h:408
void * memset(void *dst, int c, size_t len)
Definition: string.c:164
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PAlloc_Init()

void PAlloc_Init ( )
Here is the caller graph for this function:

◆ PAlloc_Release()

void PAlloc_Release ( void *  pg)

PAlloc_Release –

Deccrement the reference count for a physical page. If the reference count is zero the page will be freed.

Definition at line 265 of file palloc.c.

266{
267 PageInfo *info = PAllocGetInfo(pg);
268
270 ASSERT(info->refCount != 0);
271 info->refCount--;
272 if (info->refCount == 0)
273 PAllocFreePage(pg);
275}
static void PAllocFreePage(void *region)
Definition: palloc.c:221
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PAlloc_Retain()

void PAlloc_Retain ( void *  pg)

PAlloc_Retain –

Increment the reference count for a physical page.

Definition at line 248 of file palloc.c.

249{
250 PageInfo *info = PAllocGetInfo(pg);
251
253 ASSERT(info->refCount != 0);
254 info->refCount++;
256}
Here is the call graph for this function:

◆ Slab_Alloc()

void * Slab_Alloc ( Slab slab)

Slab_Alloc –

 Free a slab object.

 @param [in] slab Slab that the object belongs to.
 @retval NULL Could not allocate an object.
 @return Pointer to the allocated object.

Definition at line 105 of file slab.c.

106{
107 SlabElement *elem;
108
109 Spinlock_Lock(&slab->lock);
110
111 if (slab->freeObjs == 0)
112 SlabExtend(slab);
113
114 elem = LIST_FIRST(&slab->freeList);
115 if (elem != NULL) {
116 LIST_REMOVE(elem, free);
117 slab->allocs++;
118 slab->freeObjs--;
119 }
120
121 Spinlock_Unlock(&slab->lock);
122
123 return (void *)elem;
124}
int SlabExtend(Slab *slab)
Definition: slab.c:63
void free(void *buf)
Definition: malloc.c:169
Spinlock lock
Definition: kmem.h:50
uint64_t freeObjs
Definition: kmem.h:52
uint64_t allocs
Definition: kmem.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Slab_Free()

void Slab_Free ( Slab slab,
void *  region 
)

Slab_Free –

 Free a slab object.

 @param [in] slab Slab that the object belongs to.
 @param [in] region Object to free.

Definition at line 135 of file slab.c.

136{
137 Spinlock_Lock(&slab->lock);
138
139 SlabElement *elem = (SlabElement *)region;
140 LIST_INSERT_HEAD(&slab->freeList, elem, free);
141 slab->frees++;
142 slab->freeObjs++;
143
144 Spinlock_Unlock(&slab->lock);
145}
uint64_t frees
Definition: kmem.h:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Slab_Init()

void Slab_Init ( Slab slab,
const char *  name,
uintptr_t  objsz,
uintptr_t  align 
)
Here is the caller graph for this function:

◆ XMem_Allocate()

bool XMem_Allocate ( XMem xmem,
uintptr_t  length 
)

Definition at line 92 of file xmem.c.

93{
94 uint64_t off;
95
96 // We already allocated up to that size
97 if (xmem->length > length)
98 return true;
99
100 // Allocation too long
101 if (length > xmem->maxLength)
102 return false;
103
104 for (off = xmem->length; off < length; off += PGSIZE) {
105 void *pg = PAlloc_AllocPage();
106 if (pg == NULL)
107 return false;
108
109 PMap_SystemMap(DMVA2PA((uint64_t)pg), xmem->base + off, 1, 0);
110
111 xmem->length += PGSIZE;
112 }
113
114 return true;
115}
void * PAlloc_AllocPage()
Definition: palloc.c:188
bool PMap_SystemMap(uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
Definition: pmap.c:514
unsigned long uint64_t
Definition: types.h:13
uintptr_t length
Definition: xmem.c:22
uintptr_t maxLength
Definition: xmem.c:21
uintptr_t base
Definition: xmem.c:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ XMem_Destroy()

void XMem_Destroy ( XMem xmem)

Definition at line 63 of file xmem.c.

64{
65 uintptr_t off;
66 PageEntry *entry;
67
68 for (off = 0; off < xmem->length; off += PGSIZE) {
69 PMap_SystemLookup(xmem->base + off, &entry, PGSIZE);
70
71 // Compute DM
72
73 // Free Page
74 }
75
76 //PMap_SystemUnmap(virt, pages);
77}
uint64_t PageEntry
Definition: amd64.h:52
void PMap_SystemLookup(uint64_t va, PageEntry **entry, int size)
Definition: pmap.c:460
Here is the call graph for this function:

◆ XMem_GetBase()

uintptr_t XMem_GetBase ( XMem xmem)

Definition at line 80 of file xmem.c.

81{
82 return xmem->base;
83}
Here is the caller graph for this function:

◆ XMem_GetLength()

uintptr_t XMem_GetLength ( XMem xmem)

Definition at line 86 of file xmem.c.

87{
88 return xmem->length;
89}
Here is the caller graph for this function:

◆ XMem_Init()

void XMem_Init ( )

Definition at line 28 of file xmem.c.

29{
30 int r;
32
33 kprintf("Initializing XMEM ... ");
34
35 for (r = 0; r < MAX_XMEM_REGIONS; r++)
36 {
37 regions[r].inUse = false;
38 regions[r].base = MEM_XMAP_BASE + r * regionSize;
39 regions[r].maxLength = regionSize;
40 regions[r].length = 0;
41 }
42
43 kprintf("Done!\n");
44}
int kprintf(const char *fmt,...)
Definition: printf.c:210
#define MEM_XMAP_LEN
Definition: pmap.h:43
#define MEM_XMAP_BASE
Definition: pmap.h:42
XMem regions[MAX_XMEM_REGIONS]
Definition: xmem.c:25
bool inUse
Definition: xmem.c:19
#define MAX_XMEM_REGIONS
Definition: xmem.c:15
Here is the call graph for this function:
Here is the caller graph for this function:

◆ XMem_New()

XMem * XMem_New ( )

Definition at line 47 of file xmem.c.

48{
49 int r;
50
51 for (r = 0; r < MAX_XMEM_REGIONS; r++)
52 {
53 if (!regions[r].inUse) {
54 regions[r].inUse = true;
55 return &regions[r];
56 }
57 }
58
59 return NULL;
60}
Here is the caller graph for this function: