mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
Added string.h
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include <kernel.h>
|
||||
#include <string.h>
|
||||
#include <gdt.h>
|
||||
|
||||
static struct gdt_entries gdt;
|
||||
static struct gdt_pointer gdtp;
|
||||
static struct tss_pointer tss;
|
||||
struct tss_pointer* tssp = &tss;
|
||||
|
||||
void gdt_loadTss(struct tss_pointer* ptr) {
|
||||
size_t addr = (size_t)ptr;
|
||||
|
||||
gdt.tss.baseLow = (uint16_t)addr;
|
||||
gdt.tss.baseMid = (uint8_t)(addr >> 16);
|
||||
gdt.tss.flags1 = 0x89;
|
||||
gdt.tss.flags2 = 0;
|
||||
gdt.tss.baseHigh = (uint8_t)(addr >> 24);
|
||||
|
||||
__asm__ volatile ("ltr %0" : : "rm"(SEGMENT_SEL(0, 0, 7)));
|
||||
}
|
||||
|
||||
void gdt_reload(void) {
|
||||
__asm__ volatile (
|
||||
"cli\n"
|
||||
"lgdt (%0)\n"
|
||||
"ljmp $0x08, $1f\n"
|
||||
"1:\n"
|
||||
"movw $0x10, %%ax\n"
|
||||
"movw %%ax, %%ds\n"
|
||||
"movw %%ax, %%es\n"
|
||||
"movw %%ax, %%fs\n"
|
||||
"movw %%ax, %%gs\n"
|
||||
"movw %%ax, %%ss\n"
|
||||
:
|
||||
: "m" (gdtp)
|
||||
: "memory", "rax"
|
||||
);
|
||||
}
|
||||
|
||||
int gdt_init(void) {
|
||||
// Null descriptor (0)
|
||||
gdt.descriptors[0].limit = 0;
|
||||
gdt.descriptors[0].baseLow = 0;
|
||||
gdt.descriptors[0].baseMid = 0;
|
||||
gdt.descriptors[0].access = 0;
|
||||
gdt.descriptors[0].granulatiry = 0;
|
||||
gdt.descriptors[0].baseHigh = 0;
|
||||
|
||||
// Kernel code (8)
|
||||
gdt.descriptors[1].limit = 0xffff;
|
||||
gdt.descriptors[1].baseLow = 0;
|
||||
gdt.descriptors[1].baseMid = 0;
|
||||
gdt.descriptors[1].access = 0x9a;
|
||||
gdt.descriptors[1].granulatiry = 0xcf;
|
||||
gdt.descriptors[1].baseHigh = 0;
|
||||
|
||||
// Kernel data (16)
|
||||
gdt.descriptors[2].limit = 0xffff;
|
||||
gdt.descriptors[2].baseLow = 0;
|
||||
gdt.descriptors[2].baseMid = 0;
|
||||
gdt.descriptors[2].access = 0x90;
|
||||
gdt.descriptors[2].granulatiry = 0xcf;
|
||||
gdt.descriptors[2].baseHigh = 0;
|
||||
|
||||
// SYSENTER/SYSEXIT
|
||||
gdt.descriptors[3] = (struct gdt_entry){ 0 }; // (24)
|
||||
gdt.descriptors[4] = (struct gdt_entry){ 0 }; // (32)
|
||||
|
||||
// User code (40)
|
||||
gdt.descriptors[5].limit = 0;
|
||||
gdt.descriptors[5].baseLow = 0;
|
||||
gdt.descriptors[5].baseMid = 0;
|
||||
gdt.descriptors[5].access = 0xfa;
|
||||
gdt.descriptors[5].granulatiry = 0x20;
|
||||
gdt.descriptors[5].baseHigh = 0;
|
||||
|
||||
// User data (48)
|
||||
gdt.descriptors[6].limit = 0;
|
||||
gdt.descriptors[6].baseLow = 0;
|
||||
gdt.descriptors[6].baseMid = 0;
|
||||
gdt.descriptors[6].access = 0xf2;
|
||||
gdt.descriptors[6].granulatiry = 0;
|
||||
gdt.descriptors[6].baseHigh = 0;
|
||||
|
||||
// TSS (56)
|
||||
gdt.tss.length = 104;
|
||||
gdt.tss.baseLow = 0;
|
||||
gdt.tss.baseMid = 0;
|
||||
gdt.tss.flags1 = 0x89;
|
||||
gdt.tss.flags2 = 0;
|
||||
gdt.tss.baseHigh = 0;
|
||||
|
||||
gdtp.limit = sizeof(struct gdt_entries) - 1;
|
||||
gdtp.base = (uint32_t)&gdt;
|
||||
|
||||
gdt_reload();
|
||||
|
||||
memset(tssp, 0, sizeof(struct tss_pointer));
|
||||
gdt_loadTss(tssp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+4
-2
@@ -1,9 +1,10 @@
|
||||
#include <bootloader.h>
|
||||
#include <kernel.h>
|
||||
#include <serial.h>
|
||||
#include <gdt.h>
|
||||
#include <tty.h>
|
||||
|
||||
/* TODO: Commented out stuff */
|
||||
// TODO: Commented out stuff
|
||||
|
||||
__noreturn
|
||||
void kmain(void* ptr, uint32_t magic) {
|
||||
@@ -23,9 +24,10 @@ void kmain(void* ptr, uint32_t magic) {
|
||||
// pmm_init();
|
||||
// vmm_init();
|
||||
|
||||
// gdt_init();
|
||||
gdt_init();
|
||||
// isr_init();
|
||||
// pag_init();
|
||||
while (1);
|
||||
|
||||
debugf("Basic initialization complete!\n");
|
||||
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#include <kernel.h>
|
||||
#include <string.h>
|
||||
|
||||
void* memchr(const void* ptr, uint8_t val, size_t len) {
|
||||
const uint8_t* p = ptr;
|
||||
while (len--) if (*p++ == val) return (void*)(p - 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int memcmp(const void* ptr1, const void* ptr2, size_t len) {
|
||||
const uint8_t* p1 = ptr1;
|
||||
const uint8_t* p2 = ptr2;
|
||||
while (len--) if (*p1++ != *p2++) return p1[-1] - p2[-1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* memcpy(void* dst, const void* src, size_t len) {
|
||||
uint8_t* d = dst;
|
||||
const uint8_t* s = src;
|
||||
while (len--) *d++ = *s++;
|
||||
return dst;
|
||||
}
|
||||
|
||||
void* memmov(void* dst, const void* src, size_t len) {
|
||||
uint8_t* d = dst;
|
||||
const uint8_t* s = src;
|
||||
if (d < s) while (len--) *d++ = *s++;
|
||||
else while (len--) d[len] = s[len];
|
||||
return dst;
|
||||
}
|
||||
|
||||
void* memset(void* dst, uint8_t val, size_t len) {
|
||||
uint8_t* d = dst;
|
||||
while (len--) *d++ = val;
|
||||
return dst;
|
||||
}
|
||||
|
||||
char* strbrk(char* str, const char* accept) {
|
||||
while (*str) {
|
||||
if (strchr(accept, *str)) return (char*)str;
|
||||
str++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* strcat(char* dst, const char* src) {
|
||||
char* d = dst;
|
||||
while (*d) d++;
|
||||
while ((*d++ = *src++));
|
||||
return dst;
|
||||
}
|
||||
|
||||
char* strchr(const char* str, char c) {
|
||||
while (*str) if (*str++ == c) return (char*)(str - 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int strcmp(const char* str1, const char* str2) {
|
||||
while (*str1 && (*str1 == *str2)) str1++, str2++;
|
||||
return (int)(*(uint8_t*)str1 - *(uint8_t*)str2);
|
||||
}
|
||||
|
||||
char* strcpy(char* dst, const char* src) {
|
||||
char* d = dst;
|
||||
while ((*d++ = *src++));
|
||||
return dst;
|
||||
}
|
||||
|
||||
size_t strlen(const char* str) {
|
||||
const char* s = str;
|
||||
while (*s++);
|
||||
return s - str;
|
||||
}
|
||||
|
||||
size_t strspn(const char* str, const char* accept) {
|
||||
const char* s = str;
|
||||
while (*s && strchr(accept, *s)) s++;
|
||||
return s - str;
|
||||
}
|
||||
|
||||
char* strstr(const char* str1, const char* str2) {
|
||||
if (!*str2) return (char*)str1;
|
||||
for (; *str1; str1++) {
|
||||
const char* s1 = str1;
|
||||
const char* s2 = str2;
|
||||
while (*s1 && *s2 && (*s1 == *s2)) s1++, s2++;
|
||||
if (!*s2) return (char*)str1;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* strtok(char* str, const char* delim) {
|
||||
static char* next;
|
||||
if (!str) str = next;
|
||||
if (!str) return NULL;
|
||||
str += strspn(str, delim);
|
||||
if (!*str) return (next = NULL);
|
||||
char* tok = str;
|
||||
str = strbrk(str, delim);
|
||||
if (str) *str++ = '\0';
|
||||
next = str;
|
||||
return tok;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user