CS350 COS
COS
Loading...
Searching...
No Matches
xmem.c
Go to the documentation of this file.
1
2#include <stdbool.h>
3#include <stdint.h>
4
5#include <sys/kconfig.h>
6#include <sys/kassert.h>
7#include <sys/kdebug.h>
8#include <sys/kmem.h>
9
10#include <machine/amd64.h>
11#include <machine/amd64op.h>
12#include <machine/pmap.h>
13#include <machine/mp.h>
14
15#define MAX_XMEM_REGIONS 1024
16
17typedef struct XMem
18{
19 bool inUse;
24
26
27void
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}
45
46XMem *
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}
61
62void
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}
78
81{
82 return xmem->base;
83}
84
87{
88 return xmem->length;
89}
90
91bool
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}
116
117static void
118Debug_XMemStats(int argc, const char *argv[])
119{
120 int r;
121
122 kprintf("Region Nr: %16s %16s\n", "Base", "Length");
123 for (r = 0; r < MAX_XMEM_REGIONS; r++)
124 {
125 if (regions[r].inUse) {
126 kprintf("Region %2d: %016llx %016llx\n", r,
127 regions[r].base, regions[r].length);
128 }
129 }
130}
131
132REGISTER_DBGCMD(xmemstats, "XMem statistics", Debug_XMemStats);
133
uint64_t PageEntry
Definition: amd64.h:52
int kprintf(const char *fmt,...)
Definition: printf.c:210
#define REGISTER_DBGCMD(_NAME, _DESC, _FUNC)
Definition: kdebug.h:11
void * PAlloc_AllocPage()
Definition: palloc.c:188
#define PGSIZE
Definition: malloc.c:21
#define DMVA2PA(dmva)
Definition: pmap.h:47
bool PMap_SystemMap(uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
Definition: pmap.c:514
#define MEM_XMAP_LEN
Definition: pmap.h:43
#define MEM_XMAP_BASE
Definition: pmap.h:42
void PMap_SystemLookup(uint64_t va, PageEntry **entry, int size)
Definition: pmap.c:460
static uint16_t base
Definition: sercons.c:37
#define NULL
Definition: stddef.h:6
uint64_t uintptr_t
Definition: types.h:16
unsigned long uint64_t
Definition: types.h:13
uintptr_t XMem_GetLength(XMem *xmem)
Definition: xmem.c:86
XMem regions[MAX_XMEM_REGIONS]
Definition: xmem.c:25
uintptr_t length
Definition: xmem.c:22
static void Debug_XMemStats(int argc, const char *argv[])
Definition: xmem.c:118
bool inUse
Definition: xmem.c:19
uintptr_t maxLength
Definition: xmem.c:21
void XMem_Init()
Definition: xmem.c:28
XMem * XMem_New()
Definition: xmem.c:47
#define MAX_XMEM_REGIONS
Definition: xmem.c:15
bool XMem_Allocate(XMem *xmem, uintptr_t length)
Definition: xmem.c:92
uintptr_t base
Definition: xmem.c:20
uintptr_t XMem_GetBase(XMem *xmem)
Definition: xmem.c:80
void XMem_Destroy(XMem *xmem)
Definition: xmem.c:63
Definition: xmem.c:18