Skip to content

Commit 91f2f9e

Browse files
committed
Working on Buddy Allocation System.
1 parent 947a72b commit 91f2f9e

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

arch/x86/i386/early_main.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ extern "C" void early_kernel_main() {
1414
ArchX86::GDT::Init();
1515
ArchX86::IDT::Init();
1616
printk(LOG_NOTICE,"========== Finished Kernel Arch Init =========\n");
17-
asm("int $7");
18-
//kmain();
17+
kmain();
1918
}

includes/pmm.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef KERNEL_MEMORY_PMM_H
2+
#define KERNEL_MEMORY_PMM_H
3+
#include <stddef.h>
4+
#include <stdint.h>
5+
6+
#define BUDDY_BITMAPS 5
7+
#define BUDDY_STARTSIZE 12
8+
namespace Kernel {
9+
class PMM {
10+
public:
11+
static uintptr_t memsize;
12+
static uintptr_t buddy_usedpages;
13+
static uintptr_t * buddy_startpage;
14+
public:
15+
static void refreshCache();
16+
static void init();
17+
static int setPageUsed(uintptr_t * address);
18+
static int setPageFree(uintptr_t * address);
19+
};
20+
}
21+
22+
23+
#endif

kernel/init.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include <log/printk.h>
2-
2+
#include <pmm.h>
33
int kmain(/*KernelInfo * k*/) {
44
printk(LOG_INFO,"Control transfered to kernel init process.\n");
55
printk(LOG_NOTICE,"========== Started Kernel Init ===============\n");
6+
Kernel::PMM::init();
67
printk(LOG_WARN,"Kernel init process ended - Development has not made it this far yet.\n");
78
for(;;) {
89

kernel/pmm.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <pmm.h>
2+
#include <log/printk.h>
3+
4+
int ipow(int base, int exp); //TODO: A math.h
5+
6+
uintptr_t Kernel::PMM::memsize;
7+
uintptr_t Kernel::PMM::buddy_usedpages;
8+
uintptr_t * Kernel::PMM::buddy_startpage;
9+
10+
11+
void Kernel::PMM::init() {
12+
printk(LOG_INFO,"pmm: Initialsing Physical Memory Manager...\n");
13+
14+
memsize = 0xF000000; //Get memsize here.
15+
buddy_usedpages = 0;
16+
17+
for (int i = 0; i != BUDDY_BITMAPS; i++) {
18+
printk(LOG_INFO,"pmm: Creating buddy bitmap %d, resolution 0x%X, size 0x%X bytes\n",i,ipow(2,12 + i), memsize / ipow(2,12 + i));
19+
int needed_size = memsize / ipow(2,12 + i);
20+
if((needed_size & 0xFFFFF000) != needed_size) {
21+
needed_size &= 0xFFFFF000;
22+
needed_size += 0x1000;
23+
}
24+
buddy_usedpages+=needed_size / 0x1000;
25+
}
26+
printk(LOG_INFO,"pmm: Requires 0x%X pages for bookkeeping\n",buddy_usedpages);
27+
}

lib/math.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ int itoa(int value, char *sp, int radix)
5454

5555
while (tp > tmp)
5656
*sp++ = *--tp;
57-
57+
*sp++ = 0x0;
5858
return len;
5959
}
60+
61+
int ipow(int base, int exp)
62+
{
63+
if (exp < 0) {
64+
return 0;
65+
}
66+
int result = 1;
67+
while (exp)
68+
{
69+
if (exp & 1)
70+
result *= base;
71+
exp >>= 1;
72+
base *= base;
73+
}
74+
75+
return result;
76+
}

log/kernel_log.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void printk(const signed int severity, const char *fmt, ...) {
7575
break;
7676
case 'd':
7777
i = va_arg(argp, int);
78-
itoa(i, fmtbuf, 10);
78+
itoa((int)i, fmtbuf, 10);
7979
TextConsole::Print((char*)fmtbuf);
8080
break;
8181
case '%':

0 commit comments

Comments
 (0)