diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/kernel/service/memorySyscalls.c b/src/kernel/service/memorySyscalls.c index a52ec08..1711cb4 100644 --- a/src/kernel/service/memorySyscalls.c +++ b/src/kernel/service/memorySyscalls.c @@ -30,5 +30,5 @@ ADDRESS(virtualStart + i), true); } } - call->returnValue = U32(ADDRESS(virtualStart)); + call->returnValue = U32(ADDRESS(virtualStart)) + (U32(physical) & 0xFFF); } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/kernel/service/memorySyscalls.c b/src/kernel/service/memorySyscalls.c index a52ec08..1711cb4 100644 --- a/src/kernel/service/memorySyscalls.c +++ b/src/kernel/service/memorySyscalls.c @@ -30,5 +30,5 @@ ADDRESS(virtualStart + i), true); } } - call->returnValue = U32(ADDRESS(virtualStart)); + call->returnValue = U32(ADDRESS(virtualStart)) + (U32(physical) & 0xFFF); } diff --git a/src/kernel/service/service.asm b/src/kernel/service/service.asm index 8ec8d44..8584d61 100644 --- a/src/kernel/service/service.asm +++ b/src/kernel/service/service.asm @@ -19,6 +19,7 @@ mov ebx, [ebx + 20] mov cr3, eax mov eax, ebx + xor ebp, ebp sti sysexit runEnd: diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/kernel/service/memorySyscalls.c b/src/kernel/service/memorySyscalls.c index a52ec08..1711cb4 100644 --- a/src/kernel/service/memorySyscalls.c +++ b/src/kernel/service/memorySyscalls.c @@ -30,5 +30,5 @@ ADDRESS(virtualStart + i), true); } } - call->returnValue = U32(ADDRESS(virtualStart)); + call->returnValue = U32(ADDRESS(virtualStart)) + (U32(physical) & 0xFFF); } diff --git a/src/kernel/service/service.asm b/src/kernel/service/service.asm index 8ec8d44..8584d61 100644 --- a/src/kernel/service/service.asm +++ b/src/kernel/service/service.asm @@ -19,6 +19,7 @@ mov ebx, [ebx + 20] mov cr3, eax mov eax, ebx + xor ebp, ebp sti sysexit runEnd: diff --git a/src/kernel/service/service.c b/src/kernel/service/service.c index f4923a0..6903d7a 100644 --- a/src/kernel/service/service.c +++ b/src/kernel/service/service.c @@ -2,6 +2,7 @@ #include "elf.h" #include #include +#include #include #include @@ -92,8 +93,15 @@ return NULL; } -void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, Syscall *respondingTo) { +void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, ...) { + va_list valist; + va_start(valist, respondingTo); + uint32_t parameterCount = 0; + while (va_arg(valist, uint32_t) || parameterCount <= 3) { + parameterCount++; + } + va_start(valist, respondingTo); + Syscall *runCall = malloc(sizeof(Syscall)); runCall->function = 0; runCall->esp = malloc(0x1000); // todo: free this @@ -103,11 +111,11 @@ runCall->service = provider->service; runCall->resume = true; sharePage(&provider->service->pagingInfo, runCall->esp, runCall->esp); - runCall->esp += 0x1000 - 0x20; + runCall->esp += 0x1000 - 0x10 - (parameterCount * 0x4); *(void **)runCall->esp = provider->address; *(void **)(runCall->esp + 0x4) = &runEnd; - *(uint32_t *)(runCall->esp + 0x8) = data1; - *(uint32_t *)(runCall->esp + 0xC) = data2; - *(uint32_t *)(runCall->esp + 0x10) = data3; + for (uint32_t i = 0; i < parameterCount; i++) { + *(uint32_t *)(runCall->esp + 0x8 + 4 * i) = va_arg(valist, uint32_t); + } listAdd(&callsToProcess, runCall); } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/kernel/service/memorySyscalls.c b/src/kernel/service/memorySyscalls.c index a52ec08..1711cb4 100644 --- a/src/kernel/service/memorySyscalls.c +++ b/src/kernel/service/memorySyscalls.c @@ -30,5 +30,5 @@ ADDRESS(virtualStart + i), true); } } - call->returnValue = U32(ADDRESS(virtualStart)); + call->returnValue = U32(ADDRESS(virtualStart)) + (U32(physical) & 0xFFF); } diff --git a/src/kernel/service/service.asm b/src/kernel/service/service.asm index 8ec8d44..8584d61 100644 --- a/src/kernel/service/service.asm +++ b/src/kernel/service/service.asm @@ -19,6 +19,7 @@ mov ebx, [ebx + 20] mov cr3, eax mov eax, ebx + xor ebp, ebp sti sysexit runEnd: diff --git a/src/kernel/service/service.c b/src/kernel/service/service.c index f4923a0..6903d7a 100644 --- a/src/kernel/service/service.c +++ b/src/kernel/service/service.c @@ -2,6 +2,7 @@ #include "elf.h" #include #include +#include #include #include @@ -92,8 +93,15 @@ return NULL; } -void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, Syscall *respondingTo) { +void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, ...) { + va_list valist; + va_start(valist, respondingTo); + uint32_t parameterCount = 0; + while (va_arg(valist, uint32_t) || parameterCount <= 3) { + parameterCount++; + } + va_start(valist, respondingTo); + Syscall *runCall = malloc(sizeof(Syscall)); runCall->function = 0; runCall->esp = malloc(0x1000); // todo: free this @@ -103,11 +111,11 @@ runCall->service = provider->service; runCall->resume = true; sharePage(&provider->service->pagingInfo, runCall->esp, runCall->esp); - runCall->esp += 0x1000 - 0x20; + runCall->esp += 0x1000 - 0x10 - (parameterCount * 0x4); *(void **)runCall->esp = provider->address; *(void **)(runCall->esp + 0x4) = &runEnd; - *(uint32_t *)(runCall->esp + 0x8) = data1; - *(uint32_t *)(runCall->esp + 0xC) = data2; - *(uint32_t *)(runCall->esp + 0x10) = data3; + for (uint32_t i = 0; i < parameterCount; i++) { + *(uint32_t *)(runCall->esp + 0x8 + 4 * i) = va_arg(valist, uint32_t); + } listAdd(&callsToProcess, runCall); } diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index 803e1be..1e4c4c6 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -68,7 +68,7 @@ Service *providerService = listGet(services, call->parameters[0]); ServiceFunction *function = listGet(providerService->functions, call->parameters[1]); - scheduleFunction(function, call->parameters[2], call->parameters[3], - service->nameHash, call); + scheduleFunction(function, call, call->parameters[2], call->parameters[3], + service->nameHash); call->avoidReschedule = true; } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/kernel/service/memorySyscalls.c b/src/kernel/service/memorySyscalls.c index a52ec08..1711cb4 100644 --- a/src/kernel/service/memorySyscalls.c +++ b/src/kernel/service/memorySyscalls.c @@ -30,5 +30,5 @@ ADDRESS(virtualStart + i), true); } } - call->returnValue = U32(ADDRESS(virtualStart)); + call->returnValue = U32(ADDRESS(virtualStart)) + (U32(physical) & 0xFFF); } diff --git a/src/kernel/service/service.asm b/src/kernel/service/service.asm index 8ec8d44..8584d61 100644 --- a/src/kernel/service/service.asm +++ b/src/kernel/service/service.asm @@ -19,6 +19,7 @@ mov ebx, [ebx + 20] mov cr3, eax mov eax, ebx + xor ebp, ebp sti sysexit runEnd: diff --git a/src/kernel/service/service.c b/src/kernel/service/service.c index f4923a0..6903d7a 100644 --- a/src/kernel/service/service.c +++ b/src/kernel/service/service.c @@ -2,6 +2,7 @@ #include "elf.h" #include #include +#include #include #include @@ -92,8 +93,15 @@ return NULL; } -void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, Syscall *respondingTo) { +void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, ...) { + va_list valist; + va_start(valist, respondingTo); + uint32_t parameterCount = 0; + while (va_arg(valist, uint32_t) || parameterCount <= 3) { + parameterCount++; + } + va_start(valist, respondingTo); + Syscall *runCall = malloc(sizeof(Syscall)); runCall->function = 0; runCall->esp = malloc(0x1000); // todo: free this @@ -103,11 +111,11 @@ runCall->service = provider->service; runCall->resume = true; sharePage(&provider->service->pagingInfo, runCall->esp, runCall->esp); - runCall->esp += 0x1000 - 0x20; + runCall->esp += 0x1000 - 0x10 - (parameterCount * 0x4); *(void **)runCall->esp = provider->address; *(void **)(runCall->esp + 0x4) = &runEnd; - *(uint32_t *)(runCall->esp + 0x8) = data1; - *(uint32_t *)(runCall->esp + 0xC) = data2; - *(uint32_t *)(runCall->esp + 0x10) = data3; + for (uint32_t i = 0; i < parameterCount; i++) { + *(uint32_t *)(runCall->esp + 0x8 + 4 * i) = va_arg(valist, uint32_t); + } listAdd(&callsToProcess, runCall); } diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index 803e1be..1e4c4c6 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -68,7 +68,7 @@ Service *providerService = listGet(services, call->parameters[0]); ServiceFunction *function = listGet(providerService->functions, call->parameters[1]); - scheduleFunction(function, call->parameters[2], call->parameters[3], - service->nameHash, call); + scheduleFunction(function, call, call->parameters[2], call->parameters[3], + service->nameHash); call->avoidReschedule = true; } diff --git a/src/userland/crashTest/main.c b/src/userland/crashTest/main.c index ae7b506..5537ed1 100644 --- a/src/userland/crashTest/main.c +++ b/src/userland/crashTest/main.c @@ -3,8 +3,10 @@ #include #include +void testFunction() { printf("0/0 = %i", 0 / 0); } + int32_t main() { printf("trying to divide by zero now . . . "); - printf("0/0 = %i", 0 / 0); + testFunction(); return 0; } diff --git a/src/include/hlib.h b/src/include/hlib.h index 7060fc5..b6f9631 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -9,6 +9,10 @@ #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) +#define ADDRESS(pageId) PTR((pageId) << 12) +#define PAGE_ID(address) (U32(address) >> 12) +#define PAGE_OFFSET(address) (U32(address) & 0xFFF) + #define NULL PTR(0) extern uint32_t createFunction(char *name, int32_t(handler)(void *, uint32_t)); diff --git a/src/kernel/include/service.h b/src/kernel/include/service.h index 617e50c..46da745 100644 --- a/src/kernel/include/service.h +++ b/src/kernel/include/service.h @@ -35,9 +35,8 @@ extern Service *findService(char *); extern ServiceFunction *findFunction(Service *, char *); extern Service *currentService; -extern void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, - Syscall *respondingTo); +extern void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, + ...); extern void fireEvent(Event *event, uint32_t data1); extern void installKernelEvents(); diff --git a/src/kernel/include/util.h b/src/kernel/include/util.h index 2fc4417..ecf13b0 100644 --- a/src/kernel/include/util.h +++ b/src/kernel/include/util.h @@ -10,6 +10,7 @@ #define NULL PTR(0) #define MIN(x, y) (x < y ? x : y) +#define MAX(x, y) (x < y ? y : x) #define PAGE_COUNT(x) (((x - 1) / 4096) + 1) #define BITS(x) (sizeof(x) * 8) diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 1b80dc9..9e22836 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -21,6 +21,7 @@ je $ mov ecx, 0x500000 mov cr3, ecx + push ebp call onException mov eax, [temporaryESP] mov esp, eax diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 8ce4e9c..19513ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -25,15 +25,19 @@ void onInterrupt(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, uint32_t intNo) { foreach (interruptSubscriptions[intNo], ServiceFunction *, provider, - { scheduleFunction(provider, intNo, 0, 0, NULL); }) + { scheduleFunction(provider, NULL, intNo); }) ; } -void onException(void *cr3, uint32_t d, uint32_t c, uint32_t b, uint32_t a, - uint32_t intNo) { +void onException(void *ebp, void *cr3, uint32_t d, uint32_t c, uint32_t b, + uint32_t a, uint32_t intNo, uint32_t errorCode, uint32_t eip) { foreach (interruptSubscriptions[0], ServiceFunction *, provider, { - scheduleFunction(provider, intNo, 0, - ((Service *)currentSyscall->service)->nameHash, NULL); + scheduleFunction( + provider, NULL, intNo, errorCode, eip, + U32(getPhysicalAddress( + ((Service *)currentSyscall->service)->pagingInfo.pageDirectory, + ebp)), + ((Service *)currentSyscall->service)->nameHash); }) } diff --git a/src/kernel/main.c b/src/kernel/main.c index dd53e59..2d69c20 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -25,7 +25,7 @@ Service *loadProgram(char *name, Syscall *respondingTo) { Service *service = readInitrdProgram(name); ServiceFunction *provider = findFunction(service, "main"); - scheduleFunction(provider, 0, 0, 0, respondingTo); + scheduleFunction(provider, respondingTo); return service; } diff --git a/src/kernel/service/events.c b/src/kernel/service/events.c index 6e9f2f3..35f5827 100644 --- a/src/kernel/service/events.c +++ b/src/kernel/service/events.c @@ -13,9 +13,9 @@ return event; } -void fireEvent(Event *event, uint32_t data1) { +void fireEvent(Event *event, uint32_t data) { foreach (event->subscriptions, ServiceFunction *, function, - { scheduleFunction(function, data1, 0, 0, 0); }) + { scheduleFunction(function, NULL, data); }) ; } diff --git a/src/kernel/service/memorySyscalls.c b/src/kernel/service/memorySyscalls.c index a52ec08..1711cb4 100644 --- a/src/kernel/service/memorySyscalls.c +++ b/src/kernel/service/memorySyscalls.c @@ -30,5 +30,5 @@ ADDRESS(virtualStart + i), true); } } - call->returnValue = U32(ADDRESS(virtualStart)); + call->returnValue = U32(ADDRESS(virtualStart)) + (U32(physical) & 0xFFF); } diff --git a/src/kernel/service/service.asm b/src/kernel/service/service.asm index 8ec8d44..8584d61 100644 --- a/src/kernel/service/service.asm +++ b/src/kernel/service/service.asm @@ -19,6 +19,7 @@ mov ebx, [ebx + 20] mov cr3, eax mov eax, ebx + xor ebp, ebp sti sysexit runEnd: diff --git a/src/kernel/service/service.c b/src/kernel/service/service.c index f4923a0..6903d7a 100644 --- a/src/kernel/service/service.c +++ b/src/kernel/service/service.c @@ -2,6 +2,7 @@ #include "elf.h" #include #include +#include #include #include @@ -92,8 +93,15 @@ return NULL; } -void scheduleFunction(ServiceFunction *provider, uintptr_t data1, - uintptr_t data2, uintptr_t data3, Syscall *respondingTo) { +void scheduleFunction(ServiceFunction *provider, Syscall *respondingTo, ...) { + va_list valist; + va_start(valist, respondingTo); + uint32_t parameterCount = 0; + while (va_arg(valist, uint32_t) || parameterCount <= 3) { + parameterCount++; + } + va_start(valist, respondingTo); + Syscall *runCall = malloc(sizeof(Syscall)); runCall->function = 0; runCall->esp = malloc(0x1000); // todo: free this @@ -103,11 +111,11 @@ runCall->service = provider->service; runCall->resume = true; sharePage(&provider->service->pagingInfo, runCall->esp, runCall->esp); - runCall->esp += 0x1000 - 0x20; + runCall->esp += 0x1000 - 0x10 - (parameterCount * 0x4); *(void **)runCall->esp = provider->address; *(void **)(runCall->esp + 0x4) = &runEnd; - *(uint32_t *)(runCall->esp + 0x8) = data1; - *(uint32_t *)(runCall->esp + 0xC) = data2; - *(uint32_t *)(runCall->esp + 0x10) = data3; + for (uint32_t i = 0; i < parameterCount; i++) { + *(uint32_t *)(runCall->esp + 0x8 + 4 * i) = va_arg(valist, uint32_t); + } listAdd(&callsToProcess, runCall); } diff --git a/src/kernel/service/serviceSyscalls.c b/src/kernel/service/serviceSyscalls.c index 803e1be..1e4c4c6 100644 --- a/src/kernel/service/serviceSyscalls.c +++ b/src/kernel/service/serviceSyscalls.c @@ -68,7 +68,7 @@ Service *providerService = listGet(services, call->parameters[0]); ServiceFunction *function = listGet(providerService->functions, call->parameters[1]); - scheduleFunction(function, call->parameters[2], call->parameters[3], - service->nameHash, call); + scheduleFunction(function, call, call->parameters[2], call->parameters[3], + service->nameHash); call->avoidReschedule = true; } diff --git a/src/userland/crashTest/main.c b/src/userland/crashTest/main.c index ae7b506..5537ed1 100644 --- a/src/userland/crashTest/main.c +++ b/src/userland/crashTest/main.c @@ -3,8 +3,10 @@ #include #include +void testFunction() { printf("0/0 = %i", 0 / 0); } + int32_t main() { printf("trying to divide by zero now . . . "); - printf("0/0 = %i", 0 / 0); + testFunction(); return 0; } diff --git a/src/userland/log/main.c b/src/userland/log/main.c index eb92e52..a8bf9eb 100644 --- a/src/userland/log/main.c +++ b/src/userland/log/main.c @@ -66,9 +66,24 @@ printf("loading '%s' from initrd", buffer); } -void onException(uint32_t intNo, uint32_t unused, uint32_t serviceName) { +typedef struct StackFrame { + struct StackFrame *ebp; + void *eip; +} StackFrame; + +void trace(void *address, uint32_t serviceId) { printf("0x%x", address); } + +void onException(uint32_t intNo, uint32_t errorCode, void *crashAddress, + void *start, uint32_t serviceName, uint32_t serviceId) { readString(serviceName, buffer); - printf("service \"%s\" commited a %s", buffer, EXCEPTION_NAMES[intNo]); + printf("service \"%s\" encountered a %s. Stacktrace:", buffer, + EXCEPTION_NAMES[intNo]); + StackFrame *frame = requestMemory(1, NULL, start); + trace(crashAddress, serviceId); + while (frame->ebp) { + trace(frame->eip, serviceId); + frame = PAGE_OFFSET(frame->ebp) + ADDRESS(PAGE_ID(frame)); + } } int32_t main() {