diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index 324a58e..3a52801 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -2,6 +2,9 @@ #include bool stringEquals(char *string1, char *string2) { + if (string1 == string2) { + return true; + } while (*string1) { if (*string1 != *string2) { return false; @@ -9,7 +12,7 @@ string1++; string2++; } - return *string1 == *string2; + return *string2 == 0; } uint32_t strlen(char *string) { diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index 324a58e..3a52801 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -2,6 +2,9 @@ #include bool stringEquals(char *string1, char *string2) { + if (string1 == string2) { + return true; + } while (*string1) { if (*string1 != *string2) { return false; @@ -9,7 +12,7 @@ string1++; string2++; } - return *string1 == *string2; + return *string2 == 0; } uint32_t strlen(char *string) { diff --git a/src/userland/hlib/events/events.c b/src/userland/hlib/events/events.c index 4e5a1e3..8b2ed97 100644 --- a/src/userland/hlib/events/events.c +++ b/src/userland/hlib/events/events.c @@ -3,11 +3,21 @@ #include uint32_t createEvent(char *name) { - return syscall(SYS_CREATE_EVENT, U32(name), 0, 0, 0); + uintptr_t id = insertString(name); + return syscall(SYS_CREATE_EVENT, id, 0, 0, 0); +} + +uintptr_t hashString(char *string) { + uintptr_t hash = 0; + for (uintptr_t i = 0; string[i]; i++) { + hash = 257 * hash + string[i]; + } + return hash; } uint32_t getEvent(uint32_t service, char *name) { - return syscall(SYS_GET_EVENT, service, U32(name), 0, 0); + uintptr_t id = hashString(name); + return syscall(SYS_GET_EVENT, service, id, 0, 0); } void fireEvent(uint32_t eventNumber) { diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index 324a58e..3a52801 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -2,6 +2,9 @@ #include bool stringEquals(char *string1, char *string2) { + if (string1 == string2) { + return true; + } while (*string1) { if (*string1 != *string2) { return false; @@ -9,7 +12,7 @@ string1++; string2++; } - return *string1 == *string2; + return *string2 == 0; } uint32_t strlen(char *string) { diff --git a/src/userland/hlib/events/events.c b/src/userland/hlib/events/events.c index 4e5a1e3..8b2ed97 100644 --- a/src/userland/hlib/events/events.c +++ b/src/userland/hlib/events/events.c @@ -3,11 +3,21 @@ #include uint32_t createEvent(char *name) { - return syscall(SYS_CREATE_EVENT, U32(name), 0, 0, 0); + uintptr_t id = insertString(name); + return syscall(SYS_CREATE_EVENT, id, 0, 0, 0); +} + +uintptr_t hashString(char *string) { + uintptr_t hash = 0; + for (uintptr_t i = 0; string[i]; i++) { + hash = 257 * hash + string[i]; + } + return hash; } uint32_t getEvent(uint32_t service, char *name) { - return syscall(SYS_GET_EVENT, service, U32(name), 0, 0); + uintptr_t id = hashString(name); + return syscall(SYS_GET_EVENT, service, id, 0, 0); } void fireEvent(uint32_t eventNumber) { diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index c73ab04..292da46 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -37,6 +37,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; @@ -94,7 +97,7 @@ uint32_t getServiceId() { return syscall(SYS_GET_SERVICE_ID, 0, 0, 0, 0); } uintptr_t insertString(char *string) { - return syscall(SYS_INSERT_STRING, U32(string), strlen(string), 0, 0); + return syscall(SYS_INSERT_STRING, U32(string), 0, 0, 0); } uintptr_t getStringLength(uintptr_t stringId) { diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index 324a58e..3a52801 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -2,6 +2,9 @@ #include bool stringEquals(char *string1, char *string2) { + if (string1 == string2) { + return true; + } while (*string1) { if (*string1 != *string2) { return false; @@ -9,7 +12,7 @@ string1++; string2++; } - return *string1 == *string2; + return *string2 == 0; } uint32_t strlen(char *string) { diff --git a/src/userland/hlib/events/events.c b/src/userland/hlib/events/events.c index 4e5a1e3..8b2ed97 100644 --- a/src/userland/hlib/events/events.c +++ b/src/userland/hlib/events/events.c @@ -3,11 +3,21 @@ #include uint32_t createEvent(char *name) { - return syscall(SYS_CREATE_EVENT, U32(name), 0, 0, 0); + uintptr_t id = insertString(name); + return syscall(SYS_CREATE_EVENT, id, 0, 0, 0); +} + +uintptr_t hashString(char *string) { + uintptr_t hash = 0; + for (uintptr_t i = 0; string[i]; i++) { + hash = 257 * hash + string[i]; + } + return hash; } uint32_t getEvent(uint32_t service, char *name) { - return syscall(SYS_GET_EVENT, service, U32(name), 0, 0); + uintptr_t id = hashString(name); + return syscall(SYS_GET_EVENT, service, id, 0, 0); } void fireEvent(uint32_t eventNumber) { diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index c73ab04..292da46 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -37,6 +37,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; @@ -94,7 +97,7 @@ uint32_t getServiceId() { return syscall(SYS_GET_SERVICE_ID, 0, 0, 0, 0); } uintptr_t insertString(char *string) { - return syscall(SYS_INSERT_STRING, U32(string), strlen(string), 0, 0); + return syscall(SYS_INSERT_STRING, U32(string), 0, 0, 0); } uintptr_t getStringLength(uintptr_t stringId) { diff --git a/src/userland/keyboard/main.c b/src/userland/keyboard/main.c index 50b548c..54114e5 100644 --- a/src/userland/keyboard/main.c +++ b/src/userland/keyboard/main.c @@ -7,8 +7,8 @@ } int32_t main() { - log("keyboard handler installed"); uint32_t service = getService("pic"); uint32_t event = getEvent(service, "irq1"); subscribeEvent(service, event, onKey); + log("keyboard handler installed"); } diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index 324a58e..3a52801 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -2,6 +2,9 @@ #include bool stringEquals(char *string1, char *string2) { + if (string1 == string2) { + return true; + } while (*string1) { if (*string1 != *string2) { return false; @@ -9,7 +12,7 @@ string1++; string2++; } - return *string1 == *string2; + return *string2 == 0; } uint32_t strlen(char *string) { diff --git a/src/userland/hlib/events/events.c b/src/userland/hlib/events/events.c index 4e5a1e3..8b2ed97 100644 --- a/src/userland/hlib/events/events.c +++ b/src/userland/hlib/events/events.c @@ -3,11 +3,21 @@ #include uint32_t createEvent(char *name) { - return syscall(SYS_CREATE_EVENT, U32(name), 0, 0, 0); + uintptr_t id = insertString(name); + return syscall(SYS_CREATE_EVENT, id, 0, 0, 0); +} + +uintptr_t hashString(char *string) { + uintptr_t hash = 0; + for (uintptr_t i = 0; string[i]; i++) { + hash = 257 * hash + string[i]; + } + return hash; } uint32_t getEvent(uint32_t service, char *name) { - return syscall(SYS_GET_EVENT, service, U32(name), 0, 0); + uintptr_t id = hashString(name); + return syscall(SYS_GET_EVENT, service, id, 0, 0); } void fireEvent(uint32_t eventNumber) { diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index c73ab04..292da46 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -37,6 +37,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; @@ -94,7 +97,7 @@ uint32_t getServiceId() { return syscall(SYS_GET_SERVICE_ID, 0, 0, 0, 0); } uintptr_t insertString(char *string) { - return syscall(SYS_INSERT_STRING, U32(string), strlen(string), 0, 0); + return syscall(SYS_INSERT_STRING, U32(string), 0, 0, 0); } uintptr_t getStringLength(uintptr_t stringId) { diff --git a/src/userland/keyboard/main.c b/src/userland/keyboard/main.c index 50b548c..54114e5 100644 --- a/src/userland/keyboard/main.c +++ b/src/userland/keyboard/main.c @@ -7,8 +7,8 @@ } int32_t main() { - log("keyboard handler installed"); uint32_t service = getService("pic"); uint32_t event = getEvent(service, "irq1"); subscribeEvent(service, event, onKey); + log("keyboard handler installed"); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7587694..2c4f557 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -1,10 +1,6 @@ #include #include -char *testString = "hello world"; -char *lengthString = "0"; -char readBuffer[20]; - int32_t main() { loadFromInitrd("log"); loadFromInitrd("parallel"); @@ -12,16 +8,6 @@ log("honey os is alive :)"); loadFromInitrd("pic"); loadFromInitrd("keyboard"); - log(testString); - uintptr_t id = insertString(testString); - uintptr_t length = getStringLength(id); - lengthString[0] += length; - log(lengthString); - readString(id, readBuffer); - log(readBuffer); - discardString(id); - length = getStringLength(id); - lengthString[0] = '0' + length; - log(lengthString); + log("finished loading essential modules"); return 0; } diff --git a/src/include/hlib.h b/src/include/hlib.h index d14b911..4ce58ca 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -33,5 +33,6 @@ extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); extern void discardString(uintptr_t stringId); +extern uintptr_t hashString(char *string); #endif diff --git a/src/kernel/include/stringmap.h b/src/kernel/include/stringmap.h index 9ab2af0..cad8ef3 100644 --- a/src/kernel/include/stringmap.h +++ b/src/kernel/include/stringmap.h @@ -3,8 +3,8 @@ #include "stdint.h" -extern uintptr_t insertString(char *string, uintptr_t size); -extern char *retrieveString(uintptr_t stringId, uintptr_t *size); +extern uintptr_t insertString(char *string); +extern char *retrieveString(uintptr_t stringId); extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 7b94c99..0c203a7 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -22,7 +22,7 @@ void onInterrupt(void *eip, void *esp, uint32_t intNo, void *cr3) { // an external interrupt was triggered foreach (interruptSubscriptions[intNo], Provider *, provider, - { scheduleProvider(provider, PTR(intNo), 0, NULL); }) + { scheduleProvider(provider, intNo, 0, NULL); }) ; if (cr3 == PTR(0x500000)) { return; diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index a4fd4a3..8c1a07e 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,5 +1,6 @@ #include "malloc.h" #include +#include #define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) @@ -15,7 +16,8 @@ block->allocatedCoarse &= ~(1 << coarse); break; } - void *result = ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); + void *result = + ((uint8_t *)block) + block->blockSize * (32 * (uint32_t)coarse + fine); memset(result, 0, block->blockSize); return result; } @@ -36,20 +38,28 @@ last->next = block; } else { allocationData[sizeBit] = block; - block->previous = (void *)(uintptr_t)sizeBit; + block->previous = NULL; } block->magic = ALLOCATION_MAGIC; } if (block->allocatedCoarse == ~0) { goto end; } + bool abort = false; for (uint8_t coarse = 0; coarse < 32; coarse++) { for (uint8_t fine = 0; fine < 32; fine++) { + if (block->blockSize * (32 * coarse + fine + 1) > 3948) { + abort = true; + break; + } if (block->allocatedFine[coarse] & (1 << fine)) { continue; } return reserveBlock(block, coarse, fine); } + if (abort) { + break; + } } end: last = block; @@ -71,11 +81,4 @@ uint8_t fine = index % 32; block->allocatedFine[coarse] &= ~(1 << fine); block->allocatedCoarse &= ~(1 << coarse); - for (uint8_t i = 0; i < 32; i++) { - if (block->allocatedFine[coarse] & (1 << i)) { - continue; - } - block->allocatedCoarse |= 1 << coarse; - return; - } } diff --git a/src/kernel/multiboot/initrdSyscall.c b/src/kernel/multiboot/initrdSyscall.c index c8f2ed6..0ff8530 100644 --- a/src/kernel/multiboot/initrdSyscall.c +++ b/src/kernel/multiboot/initrdSyscall.c @@ -5,7 +5,7 @@ extern void loadProgram(char *name, Syscall *respondingTo); void handleLoadFromInitrdSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); Service *service = call->service; loadProgram(name, (void *)call); call->avoidReschedule = true; diff --git a/src/kernel/service/eventSyscalls.c b/src/kernel/service/eventSyscalls.c index 0f39c00..bb116e7 100644 --- a/src/kernel/service/eventSyscalls.c +++ b/src/kernel/service/eventSyscalls.c @@ -1,11 +1,14 @@ #include +#include #include void handleCreateEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[0]); + if (!name) { + return; + } Event *event = malloc(sizeof(Provider)); Service *service = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - service->pagingInfo.pageDirectory, PTR(call->parameters[0]))); event->subscriptions = NULL; event->name = name; call->returnValue = listCount(service->events); @@ -13,20 +16,21 @@ } void handleGetEventSyscall(Syscall *call) { + char *name = retrieveString(call->parameters[1]); + if (!name) { + return; + } uint32_t i = 0; Service *callService = call->service; - char *name = kernelMapPhysical(getPhysicalAddress( - callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); Service *service = listGet(services, call->parameters[0]); foreach (service->events, Event *, event, { if (stringEquals(event->name, name)) { call->returnValue = i; - break; + return; } i++; }) ; - unmapPage(name); } void handleFireEventSyscall(Syscall *call) { diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index be118d1..cb3e7c8 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -17,7 +17,7 @@ void handleGetServiceSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } @@ -34,7 +34,7 @@ void handleGetProviderSyscall(Syscall *call) { uint32_t i = 0; Service *callService = call->service; - char *name = retrieveString(call->parameters[1], NULL); + char *name = retrieveString(call->parameters[1]); if (!name) { return; } @@ -50,7 +50,7 @@ } void handleInstallSyscall(Syscall *call) { - char *name = retrieveString(call->parameters[0], NULL); + char *name = retrieveString(call->parameters[0]); if (!name) { return; } diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 7c543e4..c063f8b 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -4,50 +4,45 @@ typedef void **MapLayer; -static void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void *rootLayer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -uintptr_t hashString(char *string, uintptr_t size) { +uintptr_t hashString(char *string) { uintptr_t hash = 0; - for (uintptr_t i = 0; i < size; i++) { + for (uintptr_t i = 0; string[i]; i++) { hash = 257 * hash + string[i]; } return hash; } -uintptr_t insertString(char *string, uintptr_t size) { - uintptr_t hash = hashString(string, size); +uintptr_t insertString(char *string) { + uintptr_t hash = hashString(string); MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(hash >> startBit) & 0xF]; - if (!nextLayer) { + if (nextLayer == NULL) { nextLayer = malloc(sizeof(uintptr_t) * 16); - memset(nextLayer, 0, sizeof(uintptr_t) * 16); currentLayer[(hash >> startBit) & 0xF] = nextLayer; } currentLayer = nextLayer; } if (!currentLayer[hash >> (BITS(uintptr_t) - 4)]) { currentLayer[hash >> (BITS(uintptr_t) - 4)] = string; + } else { + free(string); } return hash; } -char *retrieveString(uintptr_t stringId, uintptr_t *size) { +char *retrieveString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { - if (size) { - *size = 0; - } return NULL; } currentLayer = nextLayer; } char *result = currentLayer[stringId >> (BITS(uintptr_t) - 4)]; - if (size) { - *size = strlen(result); - } return result; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 33e3d4e..665c765 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -6,27 +6,29 @@ Service *callService = call->service; void *string = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[0]))); - uintptr_t size = call->parameters[1]; + uintptr_t size = strlen(string); char *savedString = malloc(size + 1); memcpy(string, savedString, size); savedString[size] = 0; - call->returnValue = insertString(savedString, size); + call->returnValue = insertString(savedString); unmapPage(string); } +extern void *rootLayer[16]; + void handleReadStringLengthSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; - char *string = retrieveString(stringId, &size); - call->returnValue = size; + uintptr_t stringId = call->parameters[0]; + char *string = retrieveString(stringId); + call->returnValue = strlen(string); } void handleReadStringSyscall(Syscall *call) { - uintptr_t size, stringId = call->parameters[0]; + uintptr_t stringId = call->parameters[0]; Service *callService = call->service; void *buffer = kernelMapPhysical(getPhysicalAddress( callService->pagingInfo.pageDirectory, PTR(call->parameters[1]))); - char *string = retrieveString(stringId, &size); - memcpy(string, buffer, size + 1); + char *string = retrieveString(stringId); + memcpy(string, buffer, strlen(string) + 1); unmapPage(buffer); } diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index 324a58e..3a52801 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -2,6 +2,9 @@ #include bool stringEquals(char *string1, char *string2) { + if (string1 == string2) { + return true; + } while (*string1) { if (*string1 != *string2) { return false; @@ -9,7 +12,7 @@ string1++; string2++; } - return *string1 == *string2; + return *string2 == 0; } uint32_t strlen(char *string) { diff --git a/src/userland/hlib/events/events.c b/src/userland/hlib/events/events.c index 4e5a1e3..8b2ed97 100644 --- a/src/userland/hlib/events/events.c +++ b/src/userland/hlib/events/events.c @@ -3,11 +3,21 @@ #include uint32_t createEvent(char *name) { - return syscall(SYS_CREATE_EVENT, U32(name), 0, 0, 0); + uintptr_t id = insertString(name); + return syscall(SYS_CREATE_EVENT, id, 0, 0, 0); +} + +uintptr_t hashString(char *string) { + uintptr_t hash = 0; + for (uintptr_t i = 0; string[i]; i++) { + hash = 257 * hash + string[i]; + } + return hash; } uint32_t getEvent(uint32_t service, char *name) { - return syscall(SYS_GET_EVENT, service, U32(name), 0, 0); + uintptr_t id = hashString(name); + return syscall(SYS_GET_EVENT, service, id, 0, 0); } void fireEvent(uint32_t eventNumber) { diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index c73ab04..292da46 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -37,6 +37,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; @@ -94,7 +97,7 @@ uint32_t getServiceId() { return syscall(SYS_GET_SERVICE_ID, 0, 0, 0, 0); } uintptr_t insertString(char *string) { - return syscall(SYS_INSERT_STRING, U32(string), strlen(string), 0, 0); + return syscall(SYS_INSERT_STRING, U32(string), 0, 0, 0); } uintptr_t getStringLength(uintptr_t stringId) { diff --git a/src/userland/keyboard/main.c b/src/userland/keyboard/main.c index 50b548c..54114e5 100644 --- a/src/userland/keyboard/main.c +++ b/src/userland/keyboard/main.c @@ -7,8 +7,8 @@ } int32_t main() { - log("keyboard handler installed"); uint32_t service = getService("pic"); uint32_t event = getEvent(service, "irq1"); subscribeEvent(service, event, onKey); + log("keyboard handler installed"); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7587694..2c4f557 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -1,10 +1,6 @@ #include #include -char *testString = "hello world"; -char *lengthString = "0"; -char readBuffer[20]; - int32_t main() { loadFromInitrd("log"); loadFromInitrd("parallel"); @@ -12,16 +8,6 @@ log("honey os is alive :)"); loadFromInitrd("pic"); loadFromInitrd("keyboard"); - log(testString); - uintptr_t id = insertString(testString); - uintptr_t length = getStringLength(id); - lengthString[0] += length; - log(lengthString); - readString(id, readBuffer); - log(readBuffer); - discardString(id); - length = getStringLength(id); - lengthString[0] = '0' + length; - log(lengthString); + log("finished loading essential modules"); return 0; } diff --git a/src/userland/pic/main.c b/src/userland/pic/main.c index db6c0b7..395adc2 100644 --- a/src/userland/pic/main.c +++ b/src/userland/pic/main.c @@ -53,7 +53,7 @@ ioOut(0xA1, 0x0, 1); ioOut(0x70, ioIn(0x70, 1) | 0x80, 1); ioIn(0x71, 1); - for (uint8_t i = 0; i < sizeof(eventNames) / sizeof(void *); i++) { + for (uint8_t i = 0; i < sizeof(eventNames) / sizeof(uintptr_t); i++) { eventIds[i] = createEvent(eventNames[i]); } }