Skip to content

Commit 8781421

Browse files
committed
UPDATE
1 parent 9bfaafb commit 8781421

File tree

21 files changed

+311
-291
lines changed

21 files changed

+311
-291
lines changed

kernel/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ DEFAULT_HOST!=../default-host.sh
22
HOST?=DEFAULT_HOST
33
HOSTARCH!=../target-triplet-to-arch.sh $(HOST)
44

5-
CFLAGS?=-O0 -g -pedantic
5+
CFLAGS?=-O2 -g #-pedantic
66
CPPFLAGS?=
77
LDFLAGS?=
88
LIBS?=
@@ -18,7 +18,7 @@ CPPFLAGS:=$(CPPFLAGS) -D__is_kernel -Iinclude
1818
LDFLAGS:=$(LDFLAGS)
1919
LIBS:=$(LIBS) -nostdlib -lk -lgcc
2020

21-
ARCHDIR=arch/$(HOSTARCH)
21+
ARCHDIR=arch/i386
2222

2323
include $(ARCHDIR)/make.config
2424

kernel/arch/i386/src/drivers/keyboard.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory/memory.h>
55
#include <threading/tasking.h>
66
#include <idt/idt.h>
7+
#include <memory/mutex.h>
78
#include <drivers/keyboard.h>
89

910
void keyboard_irq();
@@ -92,7 +93,7 @@ void keyboard_init(){
9293
printf("PS/2 Keyboard init sequence activated.\n");
9394
keycache = (uint8_t*)malloc(256);
9495
memset(keycache,0,256);
95-
set_int(0x33,(uint32_t)keyboard_irq);
96+
set_int(33,(uint32_t)keyboard_irq);
9697
__kbd_enabled = 1;
9798
_kill();
9899
};
@@ -109,8 +110,10 @@ void keyboard_read_key(){
109110
}
110111
}
111112

113+
DEFINE_MUTEX(m_getkey);
112114
static char c = 0;
113115
char keyboard_get_key(){
116+
mutex_lock(&m_getkey);
114117
c = 0;
115118
if(key_loc == 0) goto out;
116119
c = *keycache;
@@ -119,6 +122,7 @@ char keyboard_get_key(){
119122
keycache[i] = keycache[i+1];
120123
}
121124
out:
125+
mutex_unlock(&m_getkey);
122126
return c;
123127
}
124128

kernel/arch/i386/src/idt/exceptions.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void exc_bound()
6363

6464
void exc_invopcode()
6565
{
66-
panic("Invalid opcode.\n");
66+
printf("Invalid opcode.\n");
6767
if(is_tasking()) {
6868
send_sig(SIG_TERM);
6969
printf("Notifying process %s (%d) with SIGTERM\n", p_name(), p_pid());
@@ -138,11 +138,16 @@ void exc_gpf()
138138

139139
void exc_pf()
140140
{
141-
panic("Page fault in %s (%d)\n",p_name(), p_pid());
141+
uint32_t esp;
142+
uint32_t cr2;
143+
asm volatile("mov (%%esp), %%eax":"=a"(esp));
144+
asm volatile("mov %%cr2, %%eax":"=a"(cr2));
145+
printf("Page fault in %s (%d); (esp)=0x%x, cr2=0x%x\n",p_name(), p_pid(),esp,cr2);
142146
if(is_tasking()) {
143147
send_sig(SIG_TERM);
144148
panic("Notifying process %s (%d) with SIGTERM\n", p_name(), p_pid());
145149
}
150+
146151
return;
147152
}
148153

kernel/arch/i386/src/idt/idt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void idt_init()
2929
idt_register_interrupt(i,(uint32_t)&__idt_default_handler);
3030
}
3131
idt_register_interrupt(0x2f,(uint32_t)&__idt_test_handler);
32-
//idt_register_interrupt(0x2e,(uint32_t)&schedule);
32+
idt_register_interrupt(0x2e,(uint32_t)&schedule);
3333
printf("Registered all interrupts to default handler\n");
3434
*(uint16_t*)idtr_location = idt_size - 1;
3535
*(uint32_t*)(idtr_location + 2) = idt_location;

kernel/arch/i386/src/memory/malloc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdint.h>
22
#include <stdio.h>
33
#include <memory/memory.h>
4+
#include <threading/pit.h>
45

56
#define MAX_PAGE_ALIGNED_ALLOCS 32
67

@@ -18,9 +19,7 @@ void mm_init(uint32_t kernel_end){
1819
pheap_end = 0x400000;
1920
pheap_begin = pheap_end - (MAX_PAGE_ALIGNED_ALLOCS * 4096);
2021
heap_end = pheap_begin;
21-
printf("MM_INIT: Use memset. heap_begin: 0x%x\n", heap_begin);
2222
memset((char*)heap_begin, 0, heap_end - heap_begin);
23-
printf("MM_INIT: Use memset done\n");
2423
pheap_desc = (uint8_t*)malloc(MAX_PAGE_ALIGNED_ALLOCS);
2524
printf("Kernel heap start at: 0x%x\n", last_alloc);
2625
}
@@ -34,7 +33,7 @@ void free(void *mem)
3433

3534
void pfree(void *mem)
3635
{
37-
if((uint32_t)mem < pheap_begin || (uint32_t)mem > pheap_end) return;
36+
if(mem < (void *)pheap_begin || mem > (void *)pheap_end) return;
3837
/* Determine which page is it */
3938
uint32_t ad = (uint32_t)mem;
4039
ad -= pheap_begin;
@@ -65,7 +64,7 @@ char* malloc(size_t size){
6564
if(a->size >= size){
6665
a->status = 1;
6766

68-
printf("RE:Allocated %d bytes from 0x%x to 0x%x\n", size, mem + sizeof(alloc_t), mem + sizeof(alloc_t) + size);
67+
//printf("RE:Allocated %d bytes from 0x%x to 0x%x\n", size, mem + sizeof(alloc_t), mem + sizeof(alloc_t) + size);
6968
memset(mem + sizeof(alloc_t), 0, size);
7069
memory_used += size + sizeof(alloc_t);
7170
return ((char*)mem + sizeof(alloc_t));
@@ -78,6 +77,7 @@ char* malloc(size_t size){
7877

7978
nalloc:;
8079
if(last_alloc+size+sizeof(alloc_t) >= heap_end){
80+
set_task(0);
8181
panic("Cannot allocate %d bytes! Out of memory.\n", size);
8282
}
8383
alloc_t *alloc = (alloc_t*)last_alloc;
@@ -87,7 +87,7 @@ char* malloc(size_t size){
8787
last_alloc += size;
8888
last_alloc += sizeof(alloc_t);
8989
last_alloc += 4;
90-
printf("Allocated %d bytes from 0x%x to 0x%x\n", size, (uint32_t)alloc + sizeof(alloc_t), last_alloc);
90+
//printf("Allocated %d bytes from 0x%x to 0x%x\n", size, (uint32_t)alloc + sizeof(alloc_t), last_alloc);
9191
memory_used += size + 4 + sizeof(alloc_t);
9292
memset((char*)(uint32_t)last_alloc + sizeof(alloc_t), 0, size);
9393
return ((char*)(uint32_t)last_alloc + sizeof(alloc_t));

kernel/arch/i386/src/memory/memutils.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <memory/memory.h>
22
#include <stdint.h>
33
#include <stddef.h>
4+
#include <memory/mutex.h>
5+
46

57
void *memset16(void *ptr, uint16_t value, size_t num) {
68
uint16_t *p = ptr;
@@ -18,11 +20,27 @@ void *memset(void *ptr, int value, size_t num) {
1820
return ptr;
1921
}
2022

21-
void*memcpy(void*dst,const void*src,size_t n){
22-
size_t i=0;
23-
for(;i<(n&~7);i+=8)*(uint64_t*)((char*)dst+i)=*(const uint64_t*)((const char*)src+i);
24-
if(i&4){ *(uint32_t*)((char*)dst+i)=*(const uint32_t*)((const char*)src+i);i+=4;}
25-
if(i&2){ *(uint16_t*)((char*)dst+i)=*(const uint16_t*)((const char*)src+i);i+=2;}
26-
if(i&1){ *(uint8_t *)((char*)dst+i)=*(const uint8_t *)((const char*)src+i);}
27-
return dst;
23+
DEFINE_MUTEX(m_memcpy)
24+
void* memcpy(const void* dest, const void* src, size_t count )
25+
{
26+
mutex_lock(&m_memcpy);
27+
char* dst8 = (char*)dest;
28+
char* src8 = (char*)src;
29+
30+
if (count & 1) {
31+
dst8[0] = src8[0];
32+
dst8 += 1;
33+
src8 += 1;
34+
}
35+
36+
count /= 2;
37+
while (count--) {
38+
dst8[0] = src8[0];
39+
dst8[1] = src8[1];
40+
41+
dst8 += 2;
42+
src8 += 2;
43+
}
44+
mutex_unlock(&m_memcpy);
45+
return (void*)dest;
2846
}

kernel/arch/i386/src/threading/pit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <threading/pit.h>
33
#include <memory/pic.h>
44
#include <stdio.h>
5+
#include <memory/hal.h>
56
#include <threading/tasking.h>
67
#include <stdint.h>
78

0 commit comments

Comments
 (0)