Skip to content

Commit 7d5ed49

Browse files
committed
Modify syscalls to fit Linux, add second heap, 'Pheap'.
1 parent 5dfdb20 commit 7d5ed49

File tree

10 files changed

+64
-18
lines changed

10 files changed

+64
-18
lines changed

apps/hw/hw

28 Bytes
Binary file not shown.

apps/hw/hw.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
static char *text = "Hello, world! LevOS and Linux cross-compatibility!\n";
12
void main()
23
{
3-
char *text = "Hello, world!\n";
4-
int file = -1;
5-
/* syscall 1: open() eax=1 ebx=stdout(1) RET: eax=file*/
6-
asm volatile("int $0x80":"=a"(file):"a"(1), "b"(1));
7-
/* syscall 2: write() eax=2 ebx=buffer ecx=file RET: none*/
8-
asm volatile("int $0x80": :"a"(2), "b"(text), "c"(file));
9-
/* syscall 0: exit() eax=0 RET: none */
10-
asm volatile("int $0x80": :"a"(0));
11-
return;
4+
/* syscall 4: write() eax=4 ebx=file ecx=buffer edx=size RET: ecx=size*/
5+
asm volatile("int $0x80": :"a"(4), "b"(0), "c"(text), "d"(52));
6+
/* syscall 1: exit() eax=1 ebx=err */
7+
asm volatile("xor %eax, %eax\n");
8+
asm volatile("int $0x80": :"a"(1), "b"(0));
9+
while(1);
1210
}

apps/hw/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
apps:
22
i586-elf-gcc -c hw.c -o hw.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
33
i586-elf-gcc -T link.ld -o hw -ffreestanding -O2 -nostdlib hw.o -lgcc
4+
cp hw /mnt/floppy/bin/hw
5+
sync

exec/elfloader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ uint8_t elf_start(uint8_t *buf, elf_priv_data *priv UNUSED)
4242
}
4343
/* Map the virtual space */
4444
uint32_t phys_loc = loader_get_unused_load_location();
45+
uint32_t cr3 = pmalloc(4096);
4546
/* Find first program header and loop through them */
4647
elf_program_header_t *ph = (elf_program_header_t *)(buf + header->e_phoff);
4748
for(int i = 0; i < header->e_phnum; i++, ph++)

exec/syscalls.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@ void syscall()
2424
//kprintf("SYSCALL! a: 0x%x b: 0x%x c:0x%x\n", eax, ebx, ecx);
2525
switch(eax)
2626
{
27-
case 0: /* syscall 0: exit() eax=0 RET: none */
27+
case 0: /* syscall 0: restart() */
28+
break;
29+
case 1: /* syscall 1: exit() eax=1 ebx=err */
2830
_kill();
31+
for(;;);
2932
break;
30-
case 1: /* syscall 1: open() eax=1 ebx=stdout(1) RET: eax=file*/
31-
asm volatile("movl $1, %eax");
33+
case 2: /* syscall 2: fork() eax = 2 ebx=pt_regs (?) */
34+
break;
35+
case 3: /* syscall 3: read() eax=3 ebx=file ecx=buf edx=size */
3236
break;
33-
case 2: /* syscall 2: write() eax=2 ebx=buffer ecx=file RET: none*/
34-
switch(ecx)
37+
case 4: /* syscall 4: write() eax=4 ebx=file ecx=buffer edx=size RET: ecx=size*/
38+
switch(ebx)
3539
{
3640
case 0:
37-
kprintf("%s\n", ebx);
41+
kprintf("%s", ecx);
3842
break;
3943
}
4044
break;
45+
case 5: /* syscall 5: open() eax=5 ebx=file ecx=flags edx=mode RET: eax=file */
46+
asm volatile("movl $1, %eax");
47+
break;
4148
default:
4249
break;
4350
}

fda.img

0 Bytes
Binary file not shown.

include/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern void mm_print_out();
1515
extern void paging_init();
1616
extern void paging_map_virtual_to_phys(uint32_t virt, uint32_t phys);
1717

18+
extern char* pmalloc(size_t size); /* page aligned alloc */
1819
extern char* malloc(size_t size);
1920
extern void free(void *mem);
2021

include/tasking.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef struct _process {
1717
uint32_t esp;
1818
uint32_t stacktop;
1919
uint32_t eip;
20+
uint32_t cr3;
2021
uint32_t state;
2122
void (*notify)(int);
2223
struct _process* next;

kernel/tasking.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void _kill()
8383
set_task(0);
8484
free((void *)c->stacktop);
8585
free(c);
86+
pfree(c->cr3);
8687
c->prev->next = c->next;
8788
c->next->prev = c->prev;
8889
set_task(1);
@@ -179,6 +180,7 @@ PROCESS* createProcess(char* name, uint32_t addr)
179180
p->state = PROCESS_STATE_ALIVE;
180181
p->notify = __notified;
181182
p->esp = (uint32_t)malloc(4096);
183+
asm volatile("mov %%cr3, %%eax":"=a"(p->cr3));
182184
uint32_t* stack = (uint32_t *)(p->esp + 4096);
183185
p->stacktop = p->esp;
184186
*--stack = 0x00000202; // eflags
@@ -242,7 +244,6 @@ void schedule_noirq()
242244
asm volatile("int $0x2e");
243245
return;
244246
}
245-
246247
void schedule()
247248
{
248249
//asm volatile("add $0xc, %esp");
@@ -259,6 +260,7 @@ void schedule()
259260
asm volatile("push %gs");
260261
asm volatile("mov %%esp, %%eax":"=a"(c->esp));
261262
c = c->next;
263+
asm volatile("mov %%eax, %%cr3": :"a"(c->cr3));
262264
asm volatile("mov %%eax, %%esp": :"a"(c->esp));
263265
asm volatile("pop %gs");
264266
asm volatile("pop %fs");

memory/malloc.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@
66
#include "../include/pit.h"
77

88
MODULE("MMU");
9+
#define MAX_PAGE_ALIGNED_ALLOCS 32
910

1011
uint32_t last_alloc = 0;
1112
uint32_t heap_end = 0;
1213
uint32_t heap_begin = 0;
13-
14+
uint32_t pheap_begin = 0;
15+
uint32_t pheap_end = 0;
16+
uint8_t *pheap_desc = 0;
1417
uint32_t memory_used = 0;
1518

1619
void mm_init(uint32_t kernel_end)
1720
{
1821
last_alloc = kernel_end + 0x1000;
1922
heap_begin = last_alloc;
20-
heap_end = 0x400000;
23+
pheap_end = 0x400000;
24+
pheap_begin = pheap_end - (MAX_PAGE_ALIGNED_ALLOCS * 4096);
25+
heap_end = pheap_begin;
2126
memset((char *)heap_begin, 0, heap_end - heap_begin);
27+
pheap_desc = (uint8_t *)malloc(MAX_PAGE_ALIGNED_ALLOCS);
2228
mprint("Kernel heap starts at 0x%x\n", last_alloc);
2329
}
2430

@@ -28,6 +34,8 @@ void mm_print_out()
2834
kprintf("Memory free: %d bytes\n", heap_end - heap_begin - memory_used);
2935
kprintf("Heap size: %d bytes\n", heap_end - heap_begin);
3036
kprintf("Heap start: 0x%x\n", heap_begin);
37+
kprintf("Heap end: 0x%x\n", heap_end);
38+
kprintf("PHeap start: 0x%x\nPHeap end: 0x%x\n", pheap_begin, pheap_end);
3139
}
3240

3341
void free(void *mem)
@@ -37,6 +45,32 @@ void free(void *mem)
3745
alloc->status = 0;
3846
}
3947

48+
void pfree(void *mem)
49+
{
50+
if(mem < pheap_begin || mem > pheap_end) return;
51+
/* Determine which page is it */
52+
uint32_t ad = (uint32_t)mem;
53+
ad -= pheap_begin;
54+
ad /= 4096;
55+
/* Now, ad has the id of the page */
56+
pheap_desc[ad] = 0;
57+
return;
58+
}
59+
60+
char* pmalloc(size_t size)
61+
{
62+
/* Loop through the avail_list */
63+
for(int i = 0; i < MAX_PAGE_ALIGNED_ALLOCS; i++)
64+
{
65+
if(pheap_desc[i]) continue;
66+
pheap_desc[i] = 1;
67+
mprint("PAllocated from 0x%x to 0x%x\n", pheap_begin + i*4096, pheap_begin + (i+1)*4096);
68+
return (char *)(pheap_begin + i*4096);
69+
}
70+
mprint("pmalloc: FATAL: failure!\n");
71+
return 0;
72+
}
73+
4074
char* malloc(size_t size)
4175
{
4276
if(!size) return 0;

0 commit comments

Comments
 (0)