diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/userland/hlib/stdio.c b/src/userland/hlib/stdio.c index 5a2ad19..4aa3547 100644 --- a/src/userland/hlib/stdio.c +++ b/src/userland/hlib/stdio.c @@ -159,6 +159,5 @@ function = getFunction(ioManager, "gets"); } uint32_t stringId = request(ioManager, function, 0, 0); - asm("nop" ::"a"(stringId)); readString(stringId, buffer); } diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/userland/hlib/stdio.c b/src/userland/hlib/stdio.c index 5a2ad19..4aa3547 100644 --- a/src/userland/hlib/stdio.c +++ b/src/userland/hlib/stdio.c @@ -159,6 +159,5 @@ function = getFunction(ioManager, "gets"); } uint32_t stringId = request(ioManager, function, 0, 0); - asm("nop" ::"a"(stringId)); readString(stringId, buffer); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7763f2b..5e9062f 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -9,5 +9,5 @@ printf("finished loading all the essential modules\n"); uint32_t id = loadFromInitrdUninitialized("shell"); requestName("ioManager", "setForeground", id, 0); - request(id, 0, 0, 0); + request(id, getFunction(id, "main"), 0, 0); } diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/userland/hlib/stdio.c b/src/userland/hlib/stdio.c index 5a2ad19..4aa3547 100644 --- a/src/userland/hlib/stdio.c +++ b/src/userland/hlib/stdio.c @@ -159,6 +159,5 @@ function = getFunction(ioManager, "gets"); } uint32_t stringId = request(ioManager, function, 0, 0); - asm("nop" ::"a"(stringId)); readString(stringId, buffer); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7763f2b..5e9062f 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -9,5 +9,5 @@ printf("finished loading all the essential modules\n"); uint32_t id = loadFromInitrdUninitialized("shell"); requestName("ioManager", "setForeground", id, 0); - request(id, 0, 0, 0); + request(id, getFunction(id, "main"), 0, 0); } diff --git a/src/userland/lspci/Makefile b/src/userland/lspci/Makefile new file mode 100644 index 0000000..4dff6a0 --- /dev/null +++ b/src/userland/lspci/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = lspci + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/userland/hlib/stdio.c b/src/userland/hlib/stdio.c index 5a2ad19..4aa3547 100644 --- a/src/userland/hlib/stdio.c +++ b/src/userland/hlib/stdio.c @@ -159,6 +159,5 @@ function = getFunction(ioManager, "gets"); } uint32_t stringId = request(ioManager, function, 0, 0); - asm("nop" ::"a"(stringId)); readString(stringId, buffer); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7763f2b..5e9062f 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -9,5 +9,5 @@ printf("finished loading all the essential modules\n"); uint32_t id = loadFromInitrdUninitialized("shell"); requestName("ioManager", "setForeground", id, 0); - request(id, 0, 0, 0); + request(id, getFunction(id, "main"), 0, 0); } diff --git a/src/userland/lspci/Makefile b/src/userland/lspci/Makefile new file mode 100644 index 0000000..4dff6a0 --- /dev/null +++ b/src/userland/lspci/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = lspci + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/lspci/main.c b/src/userland/lspci/main.c new file mode 100644 index 0000000..90f6be8 --- /dev/null +++ b/src/userland/lspci/main.c @@ -0,0 +1,145 @@ +#define ALLOC_MAIN +#include "pci.h" +#include +#include + +char *classNames[] = { + "Unclassified", + "Mass Storage Controller", + "Network controller", + "Display controller", + "Multimedia controller", + "Memory Controller", + "Bridge", + "Simple Communication controller", + "Base System Peripheral", + "Input Device controller", + "Docking station", + "Processor", + "Serial bus controller", + "Wireless controller", + "intelligent controller", + "sattelite communication controller", + "encryption controller", + "signal processing controller", + "processing accelerator", + "non-essential instrumentation", +}; + +bool checkedBuses[256]; + +uint32_t deviceCount = 0; + +ListElement *pciDevices = NULL; + +ListElement **getPciDevices() { return &pciDevices; } + +uint8_t pciConfigReadByte(uint32_t bus, uint32_t device, uint32_t function, + uint8_t offset) { + uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | + (offset & 0xFC) | 0x80000000); + ioOut(0xCF8, address, 4); + uint8_t result = (ioIn(0xCFC, 4) >> ((offset % 4) * 8)); + return result; +} + +void pciConfigWriteByte(uint32_t bus, uint32_t device, uint32_t function, + uint8_t offset, uint8_t data) { + uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | + (offset & 0xFC) | 0x80000000); + ioOut(0xCF8, address, 4); + ioOut(0xCFC, data, 1); +} + +uint16_t pciConfigReadWord(uint8_t bus, uint8_t device, uint8_t function, + uint8_t offset) { + return (uint16_t)pciConfigReadByte(bus, device, function, offset) | + ((uint16_t)pciConfigReadByte(bus, device, function, offset + 1) + << 8); +} + +uint32_t pciConfigReadInt(uint8_t bus, uint8_t device, uint8_t function, + uint8_t offset) { + return (uint32_t)pciConfigReadWord(bus, device, function, offset) | + ((uint32_t)pciConfigReadWord(bus, device, function, offset + 2) + << 16); +} + +uint8_t getHeaderType(uint8_t bus, uint8_t device, uint8_t function) { + return pciConfigReadByte(bus, device, function, 0x0E); +} + +uint16_t getVendorID(uint8_t bus, uint8_t device, uint8_t function) { + return pciConfigReadWord(bus, device, function, 0); +} + +void checkBus(uint8_t); + +void checkFunction(uint8_t bus, uint8_t device, uint8_t function) { + uint8_t class = pciConfigReadByte(bus, device, function, 0xB); + if (!class || class == 0xFF) { + return; + } + uint8_t subclass = pciConfigReadByte(bus, device, function, 0xA); + PciDevice *pciDevice = malloc(sizeof(PciDevice)); + pciDevice->bus = bus; + pciDevice->device = device; + pciDevice->function = function; + pciDevice->class = class; + pciDevice->subclass = subclass; + pciDevice->vendorId = pciConfigReadWord(bus, device, function, 0x00); + pciDevice->deviceId = pciConfigReadWord(bus, device, function, 0x02); + pciDevice->programmingInterface = + pciConfigReadByte(bus, device, function, 0x09); + pciDevice->bar0 = pciConfigReadInt(bus, device, function, 0x10); + pciDevice->bar1 = pciConfigReadInt(bus, device, function, 0x14); + pciDevice->bar2 = pciConfigReadInt(bus, device, function, 0x18); + pciDevice->bar3 = pciConfigReadInt(bus, device, function, 0x1C); + pciDevice->bar4 = pciConfigReadInt(bus, device, function, 0x20); + pciDevice->bar5 = pciConfigReadInt(bus, device, function, 0x24); + listAdd(&pciDevices, pciDevice); + printf("device at %i/%i/%i: %s\n", bus, device, function, classNames[class], + subclass); + if (class == 6 && subclass == 4) { + checkBus(pciConfigReadByte(bus, device, function, 0x19)); + } +} + +void checkDevice(uint8_t bus, uint8_t device) { + uint16_t vendorID = getVendorID(bus, device, 0); + if (vendorID == 0xFFFF) { + return; + } + if (getHeaderType(bus, device, 0) & 0x80) { + // multifunction device + for (uint16_t function = 0; function < 8; function++) { + if (getVendorID(bus, device, function) != 0xFFFF) { + checkFunction(bus, device, function); + } + } + } else { + checkFunction(bus, device, 0); + } +} + +void checkBus(uint8_t bus) { + if (checkedBuses[bus]) { + return; + } + checkedBuses[bus] = true; + for (uint16_t device = 0; device < 32; device++) { + checkDevice(bus, device); + } +} + +int32_t main() { + printf("enumerating PCI devices . . .\n"); + if (!(getHeaderType(0, 0, 0) & 0x80)) { + checkBus(0); + } else { + for (uint8_t bus = 0; bus < 8; bus++) { + checkBus(bus); + } + } + printf("enumerated %i pci devices\n", listCount(pciDevices)); +} diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/userland/hlib/stdio.c b/src/userland/hlib/stdio.c index 5a2ad19..4aa3547 100644 --- a/src/userland/hlib/stdio.c +++ b/src/userland/hlib/stdio.c @@ -159,6 +159,5 @@ function = getFunction(ioManager, "gets"); } uint32_t stringId = request(ioManager, function, 0, 0); - asm("nop" ::"a"(stringId)); readString(stringId, buffer); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7763f2b..5e9062f 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -9,5 +9,5 @@ printf("finished loading all the essential modules\n"); uint32_t id = loadFromInitrdUninitialized("shell"); requestName("ioManager", "setForeground", id, 0); - request(id, 0, 0, 0); + request(id, getFunction(id, "main"), 0, 0); } diff --git a/src/userland/lspci/Makefile b/src/userland/lspci/Makefile new file mode 100644 index 0000000..4dff6a0 --- /dev/null +++ b/src/userland/lspci/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = lspci + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/lspci/main.c b/src/userland/lspci/main.c new file mode 100644 index 0000000..90f6be8 --- /dev/null +++ b/src/userland/lspci/main.c @@ -0,0 +1,145 @@ +#define ALLOC_MAIN +#include "pci.h" +#include +#include + +char *classNames[] = { + "Unclassified", + "Mass Storage Controller", + "Network controller", + "Display controller", + "Multimedia controller", + "Memory Controller", + "Bridge", + "Simple Communication controller", + "Base System Peripheral", + "Input Device controller", + "Docking station", + "Processor", + "Serial bus controller", + "Wireless controller", + "intelligent controller", + "sattelite communication controller", + "encryption controller", + "signal processing controller", + "processing accelerator", + "non-essential instrumentation", +}; + +bool checkedBuses[256]; + +uint32_t deviceCount = 0; + +ListElement *pciDevices = NULL; + +ListElement **getPciDevices() { return &pciDevices; } + +uint8_t pciConfigReadByte(uint32_t bus, uint32_t device, uint32_t function, + uint8_t offset) { + uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | + (offset & 0xFC) | 0x80000000); + ioOut(0xCF8, address, 4); + uint8_t result = (ioIn(0xCFC, 4) >> ((offset % 4) * 8)); + return result; +} + +void pciConfigWriteByte(uint32_t bus, uint32_t device, uint32_t function, + uint8_t offset, uint8_t data) { + uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | + (offset & 0xFC) | 0x80000000); + ioOut(0xCF8, address, 4); + ioOut(0xCFC, data, 1); +} + +uint16_t pciConfigReadWord(uint8_t bus, uint8_t device, uint8_t function, + uint8_t offset) { + return (uint16_t)pciConfigReadByte(bus, device, function, offset) | + ((uint16_t)pciConfigReadByte(bus, device, function, offset + 1) + << 8); +} + +uint32_t pciConfigReadInt(uint8_t bus, uint8_t device, uint8_t function, + uint8_t offset) { + return (uint32_t)pciConfigReadWord(bus, device, function, offset) | + ((uint32_t)pciConfigReadWord(bus, device, function, offset + 2) + << 16); +} + +uint8_t getHeaderType(uint8_t bus, uint8_t device, uint8_t function) { + return pciConfigReadByte(bus, device, function, 0x0E); +} + +uint16_t getVendorID(uint8_t bus, uint8_t device, uint8_t function) { + return pciConfigReadWord(bus, device, function, 0); +} + +void checkBus(uint8_t); + +void checkFunction(uint8_t bus, uint8_t device, uint8_t function) { + uint8_t class = pciConfigReadByte(bus, device, function, 0xB); + if (!class || class == 0xFF) { + return; + } + uint8_t subclass = pciConfigReadByte(bus, device, function, 0xA); + PciDevice *pciDevice = malloc(sizeof(PciDevice)); + pciDevice->bus = bus; + pciDevice->device = device; + pciDevice->function = function; + pciDevice->class = class; + pciDevice->subclass = subclass; + pciDevice->vendorId = pciConfigReadWord(bus, device, function, 0x00); + pciDevice->deviceId = pciConfigReadWord(bus, device, function, 0x02); + pciDevice->programmingInterface = + pciConfigReadByte(bus, device, function, 0x09); + pciDevice->bar0 = pciConfigReadInt(bus, device, function, 0x10); + pciDevice->bar1 = pciConfigReadInt(bus, device, function, 0x14); + pciDevice->bar2 = pciConfigReadInt(bus, device, function, 0x18); + pciDevice->bar3 = pciConfigReadInt(bus, device, function, 0x1C); + pciDevice->bar4 = pciConfigReadInt(bus, device, function, 0x20); + pciDevice->bar5 = pciConfigReadInt(bus, device, function, 0x24); + listAdd(&pciDevices, pciDevice); + printf("device at %i/%i/%i: %s\n", bus, device, function, classNames[class], + subclass); + if (class == 6 && subclass == 4) { + checkBus(pciConfigReadByte(bus, device, function, 0x19)); + } +} + +void checkDevice(uint8_t bus, uint8_t device) { + uint16_t vendorID = getVendorID(bus, device, 0); + if (vendorID == 0xFFFF) { + return; + } + if (getHeaderType(bus, device, 0) & 0x80) { + // multifunction device + for (uint16_t function = 0; function < 8; function++) { + if (getVendorID(bus, device, function) != 0xFFFF) { + checkFunction(bus, device, function); + } + } + } else { + checkFunction(bus, device, 0); + } +} + +void checkBus(uint8_t bus) { + if (checkedBuses[bus]) { + return; + } + checkedBuses[bus] = true; + for (uint16_t device = 0; device < 32; device++) { + checkDevice(bus, device); + } +} + +int32_t main() { + printf("enumerating PCI devices . . .\n"); + if (!(getHeaderType(0, 0, 0) & 0x80)) { + checkBus(0); + } else { + for (uint8_t bus = 0; bus < 8; bus++) { + checkBus(bus); + } + } + printf("enumerated %i pci devices\n", listCount(pciDevices)); +} diff --git a/src/userland/lspci/pci.h b/src/userland/lspci/pci.h new file mode 100644 index 0000000..4e8d258 --- /dev/null +++ b/src/userland/lspci/pci.h @@ -0,0 +1,13 @@ +#ifndef PIC_H +#define PCI_H + +#include + +typedef struct { + uint8_t bus, device, function; + uint8_t class, subclass; + uint16_t deviceId, vendorId, programmingInterface; + uint32_t bar0, bar1, bar2, bar3, bar4, bar5; +} PciDevice; + +#endif diff --git a/src/include/hlib.h b/src/include/hlib.h index 5399a88..2a68800 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,6 +4,11 @@ #include #include +typedef struct ListElement { + struct ListElement *next; + void *data; +} ListElement; + #include "../userland/hlib/malloc.h" #define PTR(x) ((void *)(uintptr_t)(x)) @@ -53,4 +58,14 @@ #define MAX(x, y) (x > y ? (x) : (y)) +#define foreach(list, type, varname, ...) \ + for (ListElement *current = list; current; current = current->next) { \ + type varname = current->data; \ + __VA_ARGS__ \ + } + +extern void *listPopFirst(ListElement **list); +extern uint32_t listCount(ListElement *list); +extern void *listGet(ListElement *list, uint32_t position); + #endif diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 5c20693..97006d4 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -58,7 +58,7 @@ currentGdt[5].baseHigh = U32(&tss) >> 24; currentGdt[5].access = 0xE9; currentGdt[5].granularity = 0; - currentGdt[3].access = 0xFD; + currentGdt[3].access = 0xFE; currentGdt[4].access = 0xF2; tss.ss0 = tss.ss = 0x10; tss.esp0 = tss.esp = U32(&interruptStack) + 1024; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 2e2a6a8..730fcf0 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -38,6 +38,9 @@ extern void _printf(void *(malloc)(uint32_t), const char *format, ...); #define printf(...) _printf(malloc, __VA_ARGS__) +extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); + +#define listAdd(list, data) _listAdd(malloc, list, data) #endif diff --git a/src/kernel/memory/tar.c b/src/kernel/memory/tar.c index 75b03a4..7ec23b1 100644 --- a/src/kernel/memory/tar.c +++ b/src/kernel/memory/tar.c @@ -2,8 +2,6 @@ #include #include -#define SECTOR_COUNT(size) (size ? (size - 1) / 512 + 1 : 0) - uint32_t readOctal(char *string) { uint32_t result = 0; while (*string) { @@ -14,18 +12,14 @@ return result; } -uint32_t i = 0; - void *findTarFile(void *fileData, uint32_t tarFileSize, char *fileName) { void *currentPosition = fileData; - i++; while (currentPosition <= fileData + tarFileSize) { TarFileHeader *header = currentPosition; uint32_t fileSize = readOctal(header->fileSize); if (stringEquals(header->fileName, fileName)) { return currentPosition + 512; } - // fixme! currentPosition += 512; } return NULL; diff --git a/src/userland/hlib/list.c b/src/userland/hlib/list.c new file mode 100644 index 0000000..f01784f --- /dev/null +++ b/src/userland/hlib/list.c @@ -0,0 +1,41 @@ +#include + +void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data) { + ListElement *element = malloc(sizeof(ListElement)); + element->data = data; + element->next = NULL; + if (!*list) { + *list = element; + return; + } + ListElement *current = *list; + while (current->next) { + current = current->next; + } + current->next = element; +} + +void *listPopFirst(ListElement **list) { + if (!*list) { + return NULL; + } + ListElement *resultElement = *list; + void *result = resultElement->data; + *list = (*list)->next; + free(resultElement); + return result; +} + +uint32_t listCount(ListElement *list) { + uint32_t i = 0; + foreach (list, void *, element, { i++; }) + ; + return i; +} + +void *listGet(ListElement *list, uint32_t position) { + for (uint32_t i = 0; i < position; i++) { + list = list->next; + } + return list->data; +} diff --git a/src/userland/hlib/stdio.c b/src/userland/hlib/stdio.c index 5a2ad19..4aa3547 100644 --- a/src/userland/hlib/stdio.c +++ b/src/userland/hlib/stdio.c @@ -159,6 +159,5 @@ function = getFunction(ioManager, "gets"); } uint32_t stringId = request(ioManager, function, 0, 0); - asm("nop" ::"a"(stringId)); readString(stringId, buffer); } diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 7763f2b..5e9062f 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -9,5 +9,5 @@ printf("finished loading all the essential modules\n"); uint32_t id = loadFromInitrdUninitialized("shell"); requestName("ioManager", "setForeground", id, 0); - request(id, 0, 0, 0); + request(id, getFunction(id, "main"), 0, 0); } diff --git a/src/userland/lspci/Makefile b/src/userland/lspci/Makefile new file mode 100644 index 0000000..4dff6a0 --- /dev/null +++ b/src/userland/lspci/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = lspci + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/lspci/main.c b/src/userland/lspci/main.c new file mode 100644 index 0000000..90f6be8 --- /dev/null +++ b/src/userland/lspci/main.c @@ -0,0 +1,145 @@ +#define ALLOC_MAIN +#include "pci.h" +#include +#include + +char *classNames[] = { + "Unclassified", + "Mass Storage Controller", + "Network controller", + "Display controller", + "Multimedia controller", + "Memory Controller", + "Bridge", + "Simple Communication controller", + "Base System Peripheral", + "Input Device controller", + "Docking station", + "Processor", + "Serial bus controller", + "Wireless controller", + "intelligent controller", + "sattelite communication controller", + "encryption controller", + "signal processing controller", + "processing accelerator", + "non-essential instrumentation", +}; + +bool checkedBuses[256]; + +uint32_t deviceCount = 0; + +ListElement *pciDevices = NULL; + +ListElement **getPciDevices() { return &pciDevices; } + +uint8_t pciConfigReadByte(uint32_t bus, uint32_t device, uint32_t function, + uint8_t offset) { + uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | + (offset & 0xFC) | 0x80000000); + ioOut(0xCF8, address, 4); + uint8_t result = (ioIn(0xCFC, 4) >> ((offset % 4) * 8)); + return result; +} + +void pciConfigWriteByte(uint32_t bus, uint32_t device, uint32_t function, + uint8_t offset, uint8_t data) { + uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | + (offset & 0xFC) | 0x80000000); + ioOut(0xCF8, address, 4); + ioOut(0xCFC, data, 1); +} + +uint16_t pciConfigReadWord(uint8_t bus, uint8_t device, uint8_t function, + uint8_t offset) { + return (uint16_t)pciConfigReadByte(bus, device, function, offset) | + ((uint16_t)pciConfigReadByte(bus, device, function, offset + 1) + << 8); +} + +uint32_t pciConfigReadInt(uint8_t bus, uint8_t device, uint8_t function, + uint8_t offset) { + return (uint32_t)pciConfigReadWord(bus, device, function, offset) | + ((uint32_t)pciConfigReadWord(bus, device, function, offset + 2) + << 16); +} + +uint8_t getHeaderType(uint8_t bus, uint8_t device, uint8_t function) { + return pciConfigReadByte(bus, device, function, 0x0E); +} + +uint16_t getVendorID(uint8_t bus, uint8_t device, uint8_t function) { + return pciConfigReadWord(bus, device, function, 0); +} + +void checkBus(uint8_t); + +void checkFunction(uint8_t bus, uint8_t device, uint8_t function) { + uint8_t class = pciConfigReadByte(bus, device, function, 0xB); + if (!class || class == 0xFF) { + return; + } + uint8_t subclass = pciConfigReadByte(bus, device, function, 0xA); + PciDevice *pciDevice = malloc(sizeof(PciDevice)); + pciDevice->bus = bus; + pciDevice->device = device; + pciDevice->function = function; + pciDevice->class = class; + pciDevice->subclass = subclass; + pciDevice->vendorId = pciConfigReadWord(bus, device, function, 0x00); + pciDevice->deviceId = pciConfigReadWord(bus, device, function, 0x02); + pciDevice->programmingInterface = + pciConfigReadByte(bus, device, function, 0x09); + pciDevice->bar0 = pciConfigReadInt(bus, device, function, 0x10); + pciDevice->bar1 = pciConfigReadInt(bus, device, function, 0x14); + pciDevice->bar2 = pciConfigReadInt(bus, device, function, 0x18); + pciDevice->bar3 = pciConfigReadInt(bus, device, function, 0x1C); + pciDevice->bar4 = pciConfigReadInt(bus, device, function, 0x20); + pciDevice->bar5 = pciConfigReadInt(bus, device, function, 0x24); + listAdd(&pciDevices, pciDevice); + printf("device at %i/%i/%i: %s\n", bus, device, function, classNames[class], + subclass); + if (class == 6 && subclass == 4) { + checkBus(pciConfigReadByte(bus, device, function, 0x19)); + } +} + +void checkDevice(uint8_t bus, uint8_t device) { + uint16_t vendorID = getVendorID(bus, device, 0); + if (vendorID == 0xFFFF) { + return; + } + if (getHeaderType(bus, device, 0) & 0x80) { + // multifunction device + for (uint16_t function = 0; function < 8; function++) { + if (getVendorID(bus, device, function) != 0xFFFF) { + checkFunction(bus, device, function); + } + } + } else { + checkFunction(bus, device, 0); + } +} + +void checkBus(uint8_t bus) { + if (checkedBuses[bus]) { + return; + } + checkedBuses[bus] = true; + for (uint16_t device = 0; device < 32; device++) { + checkDevice(bus, device); + } +} + +int32_t main() { + printf("enumerating PCI devices . . .\n"); + if (!(getHeaderType(0, 0, 0) & 0x80)) { + checkBus(0); + } else { + for (uint8_t bus = 0; bus < 8; bus++) { + checkBus(bus); + } + } + printf("enumerated %i pci devices\n", listCount(pciDevices)); +} diff --git a/src/userland/lspci/pci.h b/src/userland/lspci/pci.h new file mode 100644 index 0000000..4e8d258 --- /dev/null +++ b/src/userland/lspci/pci.h @@ -0,0 +1,13 @@ +#ifndef PIC_H +#define PCI_H + +#include + +typedef struct { + uint8_t bus, device, function; + uint8_t class, subclass; + uint16_t deviceId, vendorId, programmingInterface; + uint32_t bar0, bar1, bar2, bar3, bar4, bar5; +} PciDevice; + +#endif diff --git a/src/userland/pic/main.c b/src/userland/pic/main.c index 7ceffa1..5d90a70 100644 --- a/src/userland/pic/main.c +++ b/src/userland/pic/main.c @@ -42,7 +42,7 @@ sentPic2EOI = true; ioOut(PIC2, 0x20, 1); } - fireEvent(eventIds[i]); + fireEvent(eventIds[i], 0); } }