Fix atomics and make it compile

This commit is contained in:
Curle 2021-07-15 04:43:46 +01:00
parent 138c026b9a
commit 770e040383
9 changed files with 67 additions and 61 deletions

View File

@ -106,8 +106,8 @@ void DrawEditor(EditorState* currentState);
void DrawHeader(); void DrawHeader();
void DrawBackground(); void DrawBackground();
void DrawTextArea(); void DrawTextArea();
void DrawTextLine();
void DrawText(); void DrawText();
void DrawLine();
void DrawMenus(); void DrawMenus();
void DrawBoxes(); void DrawBoxes();
void DrawBoxShadows(); void DrawBoxShadows();

View File

@ -55,11 +55,11 @@ typedef struct {
} __attribute__((packed)) psf_t; } __attribute__((packed)) psf_t;
size_t KernelAddr; extern size_t KernelAddr;
size_t KernelEnd; extern size_t KernelEnd;
size_t MemoryPages; extern size_t MemoryPages;
size_t FreeMemorySize; extern size_t FreeMemorySize;
size_t FullMemorySize; extern size_t FullMemorySize;
void SetupExtensions(); void SetupExtensions();
@ -81,7 +81,7 @@ int ParseKernelHeader(size_t InitrdPtr);
int Main(); int Main();
void Exit(); void Exit(int code);
void SomethingWentWrong(const char* Message); void SomethingWentWrong(const char* Message);

View File

@ -1,9 +1,4 @@
#pragma once #pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <kernel/system/descriptors.h> #include <kernel/system/descriptors.h>
/************************ /************************
@ -13,8 +8,9 @@ extern "C" {
#define PAUSE __asm__ __volatile__("pause") #define PAUSE __asm__ __volatile__("pause")
uint8_t kbdSBuffer[8]; #ifdef __cplusplus
uint8_t InputBuffer[128]; extern "C" {
#endif
typedef struct { typedef struct {
uint8_t ACK; uint8_t ACK;
@ -24,7 +20,7 @@ typedef struct {
uint8_t Error; uint8_t Error;
} KBD_FLAGS; } KBD_FLAGS;
KBD_FLAGS KbdFlags; extern KBD_FLAGS KbdFlags;
DESC_TBL ReadGDT(void); DESC_TBL ReadGDT(void);

View File

@ -1,8 +1,6 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#include <stdatomic.h>
// TODO: <atomic> for cplusplus.
/************************ /************************
*** Team Kitty, 2020 *** *** Team Kitty, 2020 ***
@ -25,8 +23,8 @@ extern "C" {
*/ */
typedef struct { typedef struct {
atomic_size_t NowServing; size_t NowServing;
atomic_size_t NextTicket; size_t NextTicket;
} ticketlock_t; } ticketlock_t;
#define NEW_TICKETLOCK() (ticketlock_t{0}) #define NEW_TICKETLOCK() (ticketlock_t{0})

View File

@ -19,11 +19,12 @@ extern "C" {
static bool KernelLoaded = false; static bool KernelLoaded = false;
address_space_t KernelAddressSpace;
size_t KernelAddr = (size_t) &LoadAddr; size_t KernelAddr = (size_t) &LoadAddr;
size_t KernelEnd = (size_t) &end; size_t KernelEnd = (size_t) &end;
address_space_t KernelAddressSpace;
void PrintPressedChar(KeyboardData data); void PrintPressedChar(KeyboardData data);
int CharPrinterCallbackID; int CharPrinterCallbackID;
void TrackInternalBuffer(KeyboardData data); void TrackInternalBuffer(KeyboardData data);
@ -145,7 +146,6 @@ void SomethingWentWrong(const char* Message) {
void Exit(int ExitCode) { void Exit(int ExitCode) {
SerialPrintf("Kernel stopped with code %x\r\n", ExitCode); SerialPrintf("Kernel stopped with code %x\r\n", ExitCode);
} }
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,27 +1,29 @@
#include <kernel/chroma.h> #include <lainlib/mutex/ticketlock.h>
#include <lainlib/lainlib.h>
#define PAUSE __asm__ __volatile__("pause")
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void TicketLock(ticketlock_t* Lock) { void TicketLock(ticketlock_t* Lock) {
size_t Ticket = atomic_fetch_add_explicit(&Lock->NextTicket, 1, memory_order_relaxed); size_t Ticket = __atomic_fetch_add(&Lock->NextTicket, 1, __ATOMIC_RELAXED);
__sync_synchronize();
while(atomic_load_explicit(&Lock->NowServing, memory_order_acquire) != Ticket) { while(__atomic_load_8(&Lock->NowServing, __ATOMIC_ACQUIRE) != Ticket) {
PAUSE; PAUSE;
} }
} }
bool TicketAttemptLock(ticketlock_t* Lock) { bool TicketAttemptLock(ticketlock_t* Lock) {
size_t Ticket = atomic_load_explicit(&Lock->NowServing, memory_order_relaxed); size_t Ticket = __atomic_load_8(&Lock->NowServing, __ATOMIC_RELAXED);
__sync_synchronize();
return atomic_compare_exchange_strong_explicit(&Lock->NowServing, &Ticket, Ticket + 1, memory_order_acquire, memory_order_relaxed); return __atomic_compare_exchange_8(&Lock->NowServing, &Ticket, Ticket + 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
} }
void TicketUnlock(ticketlock_t* Lock) { void TicketUnlock(ticketlock_t* Lock) {
size_t NextTicket = atomic_load_explicit(&Lock->NowServing, memory_order_relaxed) + 1; size_t NextTicket = __atomic_load_8(&Lock->NowServing, __ATOMIC_RELAXED) + 1;
atomic_store_explicit(&Lock->NowServing, NextTicket, memory_order_release); __sync_synchronize();
__atomic_store_8(&Lock->NowServing, NextTicket, __ATOMIC_RELEASE);
} }
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -20,6 +20,8 @@ extern "C" {
* *
*/ */
KBD_FLAGS KbdFlags;
char keys[128] = { char keys[128] = {
0, 27, 0, 27,
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',

View File

@ -32,6 +32,10 @@ extern "C" {
uint8_t* MemoryStart; uint8_t* MemoryStart;
size_t MemoryBuckets; size_t MemoryBuckets;
size_t MemoryPages = 0;
size_t FreeMemorySize = 0;
size_t FullMemorySize = 0;
static buddy_t LowBuddy = { static buddy_t LowBuddy = {
.MaxOrder = 32, .MaxOrder = 32,
.Base = (directptr_t) DIRECT_REGION, .Base = (directptr_t) DIRECT_REGION,
@ -155,9 +159,6 @@ static directptr_t BuddyAllocate(buddy_t* Buddy, size_t Size) {
void InitMemoryManager() { void InitMemoryManager() {
SerialPrintf("[ Mem] Counting memory..\r\n"); SerialPrintf("[ Mem] Counting memory..\r\n");
FreeMemorySize = 0;
FullMemorySize = 0;
size_t MemMapEntryCount = 0; size_t MemMapEntryCount = 0;
MMapEnt* MemMap = &bootldr.mmap; MMapEnt* MemMap = &bootldr.mmap;

View File

@ -18,7 +18,7 @@ extern "C" {
* This file also provides SerialPrintf. * This file also provides SerialPrintf.
*/ */
static void NumToStr(char* Buffer, size_t Num, size_t Base) { void NumToStr(char* Buffer, size_t Num, size_t Base) {
size_t Temp, i = 0, j = 0; size_t Temp, i = 0, j = 0;
do { do {
@ -54,7 +54,7 @@ int SerialPrintf(const char* Format, ...) {
switch(*Format) { switch(*Format) {
case 'c': case 'c': {
Format++; Format++;
char c = (char) va_arg(Parameters, int); char c = (char) va_arg(Parameters, int);
@ -63,7 +63,8 @@ int SerialPrintf(const char* Format, ...) {
CharsWritten++; CharsWritten++;
break; break;
case 's': }
case 's': {
Format++; Format++;
const char* Str = va_arg(Parameters, char*); const char* Str = va_arg(Parameters, char*);
@ -76,10 +77,11 @@ int SerialPrintf(const char* Format, ...) {
CharsWritten += Len; CharsWritten += Len;
break; break;
}
case 'd': case 'd':
case 'u': case 'u':
case 'p': case 'p':
case 'x': case 'x': {
Num = va_arg(Parameters, size_t); Num = va_arg(Parameters, size_t);
Base = 0; Base = 0;
@ -93,12 +95,13 @@ int SerialPrintf(const char* Format, ...) {
Format++; Format++;
NumToStr(BufferStr, Num, Base); NumToStr(BufferStr, Num, Base);
Len = strlen(BufferStr); size_t Len = strlen(BufferStr);
WriteSerialString(BufferStr, Len); WriteSerialString(BufferStr, Len);
CharsWritten += Len; CharsWritten += Len;
break; break;
}
//case 'p': //case 'p':
//uint8_t Base = 16; //uint8_t Base = 16;
//size_t Dest = (uintptr_t) va_arg(Parameters, void*); //size_t Dest = (uintptr_t) va_arg(Parameters, void*);
@ -117,7 +120,7 @@ int SerialPrintf(const char* Format, ...) {
return CharsWritten; return CharsWritten;
} }
size_t ParseEnglishColor(const char* Name); size_t ParseEnglishColor(char* Name);
size_t ParseHexColor(const char* Name, bool bgFlag); size_t ParseHexColor(const char* Name, bool bgFlag);
int Printf(const char* Format, ...) { int Printf(const char* Format, ...) {
@ -137,7 +140,7 @@ int Printf(const char* Format, ...) {
Format++; Format++;
switch(*Format) { switch(*Format) {
case 'c': case 'c': {
Format++; Format++;
char c = (char) va_arg(Parameters, int); char c = (char) va_arg(Parameters, int);
@ -146,7 +149,8 @@ int Printf(const char* Format, ...) {
CharsWritten++; CharsWritten++;
break; break;
case 's': }
case 's': {
Format++; Format++;
const char* Str = va_arg(Parameters, char*); const char* Str = va_arg(Parameters, char*);
@ -159,10 +163,11 @@ int Printf(const char* Format, ...) {
CharsWritten += Len; CharsWritten += Len;
break; break;
}
case 'd': case 'd':
case 'u': case 'u':
case 'p': case 'p':
case 'x': case 'x': {
Num = va_arg(Parameters, size_t); Num = va_arg(Parameters, size_t);
Base = 0; Base = 0;
@ -176,12 +181,13 @@ int Printf(const char* Format, ...) {
Format++; Format++;
NumToStr(BufferStr, Num, Base); NumToStr(BufferStr, Num, Base);
Len = strlen(BufferStr); size_t Len = strlen(BufferStr);
WriteString(BufferStr); WriteString(BufferStr);
CharsWritten += Len; CharsWritten += Len;
break; break;
}
//case 'p': //case 'p':
//uint8_t Base = 16; //uint8_t Base = 16;
//size_t Dest = (uintptr_t) va_arg(Parameters, void*); //size_t Dest = (uintptr_t) va_arg(Parameters, void*);
@ -194,22 +200,22 @@ int Printf(const char* Format, ...) {
Format++; // Skip backslash Format++; // Skip backslash
switch(*Format) { switch(*Format) {
case '$': case '$': {
// COLOR // COLOR
Format ++; // Skip $ and { Format ++; // Skip $ and {
size_t ContentLength = 0;
size_t Color = 0; size_t Color = 0;
bool bgFlag = false; bool bgFlag = false;
switch(*Format) { switch(*Format) {
case '[': case '[':
bgFlag = true; bgFlag = true;
[[fallthrough]];
// bg // bg
case '{': case '{':
// fg // fg
Format++; Format++;
if(*Format == '<') { if(*Format == '<') {
Color = ParseEnglishColor(++Format); Color = ParseEnglishColor((char*) ++Format);
Format++; Format++;
} else { } else {
Color = ParseHexColor(++Format, bgFlag); Color = ParseHexColor(++Format, bgFlag);
@ -227,6 +233,7 @@ int Printf(const char* Format, ...) {
Format++; Format++;
break; break;
}
case 'f': case 'f':
// FORMAT // FORMAT
break; break;
@ -244,24 +251,24 @@ int Printf(const char* Format, ...) {
} }
size_t ParseEnglishColor(const char* Stream) { size_t ParseEnglishColor(char* Stream) {
if(strcmp("red", Stream)) if(strcmp(Stream, "red"))
return 0xFF0000; return 0xFF0000;
if(strcmp("green", Stream)) if(strcmp(Stream, "green"))
return 0xFF00; return 0xFF00;
if(strcmp("blue", Stream)) if(strcmp(Stream, "blue"))
return 0xFF; return 0xFF;
if(strcmp("yellow", Stream)) if(strcmp(Stream, "yellow"))
return 0xFFFF00; return 0xFFFF00;
if(strcmp("cyan", Stream)) if(strcmp(Stream, "cyan"))
return 0xFFFF; return 0xFFFF;
if(strcmp("magenta", Stream)) if(strcmp(Stream, "magenta"))
return 0xFF00FF; return 0xFF00FF;
if(strcmp("beans", Stream)) if(strcmp(Stream, "beans"))
return 0xAA11CC; return 0xAA11CC;
if(strcmp("forgeb", Stream)) if(strcmp(Stream, "forgeb"))
return 0x1E2D42; return 0x1E2D42;
if(strcmp("forgey", Stream)) if(strcmp(Stream, "forgey"))
return 0xE0A969; return 0xE0A969;
return 0xFFFFFF; return 0xFFFFFF;
} }
@ -270,8 +277,8 @@ size_t ParseHexColor(const char* Stream, bool bgFlag) {
size_t val = 0; size_t val = 0;
char c; char c;
while ((*Stream != bgFlag ? ']' : '}') && (c = *Stream++)) { while ((*Stream != (bgFlag ? ']' : '}')) && (c = *Stream++)) {
char v = (c & 0xF) + (c >> 6) | ((c >> 3) & 0x8); char v = ((c & 0xF) + (c >> 6)) | ((c >> 3) & 0x8);
val = (val << 4) | (size_t) v; val = (val << 4) | (size_t) v;
} }