Fixing broken stuff
This commit is contained in:
parent
98c2dd8502
commit
52648a9907
|
@ -13,7 +13,7 @@
|
||||||
CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc"
|
CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc"
|
||||||
CC2NAME=gcc
|
CC2NAME=gcc
|
||||||
COPTIM=-O2
|
COPTIM=-O2
|
||||||
CWARNS=-Wall -Wextra -Wshadow -Wpedantic
|
CWARNS=-Wall -Wextra -Wshadow // -Wpedantic
|
||||||
CINCLUDES=-isystem./kaleid/include
|
CINCLUDES=-isystem./kaleid/include
|
||||||
|
|
||||||
CFLAGS1=-std=gnu11 -nostdlib -ffreestanding -mcmodel=large
|
CFLAGS1=-std=gnu11 -nostdlib -ffreestanding -mcmodel=large
|
||||||
|
|
|
@ -12,8 +12,10 @@
|
||||||
//
|
//
|
||||||
// Digits table for bases <=36 (unused)
|
// Digits table for bases <=36 (unused)
|
||||||
//
|
//
|
||||||
|
#if 0
|
||||||
static const char digits[] =
|
static const char digits[] =
|
||||||
"0123456789abcdefghijklmnopqrstuvwxyz";
|
"0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
|
#endif
|
||||||
|
|
||||||
//
|
//
|
||||||
// Integer to string in any base between 2 and 36 (included)
|
// Integer to string in any base between 2 and 36 (included)
|
||||||
|
@ -21,29 +23,35 @@ static const char digits[] =
|
||||||
|
|
||||||
#if defined(_NEED_ITOA)
|
#if defined(_NEED_ITOA)
|
||||||
|
|
||||||
char *itoa(int i, char *str, int base)
|
|
||||||
#define _IL_MIN INT_MIN
|
#define _IL_MIN INT_MIN
|
||||||
#define _IL_MIN_STRING "-2147483648"
|
#define _IL_MIN_STRING "-2147483648"
|
||||||
|
char *itoa(int i, char *str, int base)
|
||||||
|
{
|
||||||
|
int rem;
|
||||||
|
|
||||||
#elif defined(_NEED_LTOA)
|
#elif defined(_NEED_LTOA)
|
||||||
|
|
||||||
char *ltoa(long i, char *str, int base)
|
|
||||||
#define _IL_MIN LONG_MIN
|
#define _IL_MIN LONG_MIN
|
||||||
#define _IL_MIN_STRING "-9223372036854775808"
|
#define _IL_MIN_STRING "-9223372036854775808"
|
||||||
|
char *ltoa(long i, char *str, int base)
|
||||||
|
{
|
||||||
|
long rem;
|
||||||
|
|
||||||
#elif defined(_NEED_UTOA)
|
#elif defined(_NEED_UTOA)
|
||||||
|
|
||||||
char *utoa(uint i, char *str, int base)
|
char *utoa(uint i, char *str, int base)
|
||||||
|
{
|
||||||
|
uint rem;
|
||||||
|
|
||||||
#elif defined(_NEED_ULTOA)
|
#elif defined(_NEED_ULTOA)
|
||||||
|
|
||||||
char *ultoa(ulong i, char *str, int base)
|
char *ultoa(ulong i, char *str, int base)
|
||||||
|
{
|
||||||
|
ulong rem;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#error "What am I supposed to declare?"
|
#error "What am I supposed to declare?"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{
|
|
||||||
char *orig = str;
|
char *orig = str;
|
||||||
|
|
||||||
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
|
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
|
||||||
|
@ -72,7 +80,7 @@ char *ultoa(ulong i, char *str, int base)
|
||||||
//
|
//
|
||||||
if (base < 2 || base > 36) {
|
if (base < 2 || base > 36) {
|
||||||
__set_errno(EINVAL);
|
__set_errno(EINVAL);
|
||||||
*orig = '\0;
|
*orig = '\0';
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,8 @@ static ulong next = 7756;
|
||||||
//
|
//
|
||||||
int rand(void)
|
int rand(void)
|
||||||
{
|
{
|
||||||
next = next * 1103515245 + 12345;
|
next = next * 1103515245 + 12347;
|
||||||
return (uint)(next / 65536) % INT_MAX;
|
return (uint)(next / 65536);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -12,6 +12,9 @@
|
||||||
//
|
//
|
||||||
// Format str according to fmt using ellipsed arguments
|
// Format str according to fmt using ellipsed arguments
|
||||||
//
|
//
|
||||||
|
// BE CAREFUL when using this
|
||||||
|
// you need to know for sure an overflow won't happen
|
||||||
|
//
|
||||||
int sprintf(char *str, const char *fmt, ...)
|
int sprintf(char *str, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -31,6 +34,7 @@ int vsprintf(char *str, const char *fmt, va_list ap)
|
||||||
|
|
||||||
//
|
//
|
||||||
// (v)sprintf() but with a size limit: no more than n bytes are written in str
|
// (v)sprintf() but with a size limit: no more than n bytes are written in str
|
||||||
|
// Always null-terminate str
|
||||||
//
|
//
|
||||||
int snprintf(char *str, size_t n, const char *fmt, ...)
|
int snprintf(char *str, size_t n, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
|
|
@ -292,7 +292,7 @@ char *strncat(char *restrict dest, const char *restrict src, size_t n)
|
||||||
// Always null-terminates, and returne TRUE or FALSE depending on whether
|
// Always null-terminates, and returne TRUE or FALSE depending on whether
|
||||||
// regular strcat() would have null-terminated this string, or not
|
// regular strcat() would have null-terminated this string, or not
|
||||||
//
|
//
|
||||||
int *strnzcat(char *restrict dest, const char *restrict src, size_t n)
|
int strnzcat(char *restrict dest, const char *restrict src, size_t n)
|
||||||
{
|
{
|
||||||
size_t it, off = 0;
|
size_t it, off = 0;
|
||||||
|
|
||||||
|
|
|
@ -147,7 +147,7 @@ int strnzcpy(char *restrict, const char *restrict, size_t);
|
||||||
|
|
||||||
char *strcat (char *restrict, const char *restrict);
|
char *strcat (char *restrict, const char *restrict);
|
||||||
char *strncat (char *restrict, const char *restrict, size_t);
|
char *strncat (char *restrict, const char *restrict, size_t);
|
||||||
int *strnzcat(char *restrict, const char *restrict, size_t);
|
int strnzcat(char *restrict, const char *restrict, size_t);
|
||||||
|
|
||||||
char *strrev(char *restrict, const char *restrict);
|
char *strrev(char *restrict, const char *restrict);
|
||||||
char *strrev2(char *);
|
char *strrev2(char *);
|
||||||
|
|
|
@ -26,6 +26,10 @@
|
||||||
#define NULL 0L
|
#define NULL 0L
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef INITOK
|
||||||
|
#define INITOK ((unsigned int)0xCAFEBABE)
|
||||||
|
#endif
|
||||||
|
|
||||||
//------------------------------------------//
|
//------------------------------------------//
|
||||||
// Keywords //
|
// Keywords //
|
||||||
//------------------------------------------//
|
//------------------------------------------//
|
||||||
|
|
|
@ -38,6 +38,13 @@
|
||||||
#include <kernel/kernterm.h>
|
#include <kernel/kernterm.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// not ready for kernel compilation
|
||||||
|
#ifndef _KALEID_KERNEL
|
||||||
|
#ifndef _KALKERN_SCHED_H
|
||||||
|
#include <kernel/kernsched.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
//------------------------------------------//
|
//------------------------------------------//
|
||||||
// End of header //
|
// End of header //
|
||||||
//------------------------------------------//
|
//------------------------------------------//
|
||||||
|
|
|
@ -59,7 +59,6 @@ typedef enum {
|
||||||
// Multiprocessor misc. //
|
// Multiprocessor misc. //
|
||||||
//------------------------------------------//
|
//------------------------------------------//
|
||||||
|
|
||||||
|
|
||||||
#ifndef INITOK
|
#ifndef INITOK
|
||||||
#define INITOK ((unsigned int)0xCAFEBABE)
|
#define INITOK ((unsigned int)0xCAFEBABE)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
|
|
||||||
sched-test:
|
sched-test:
|
||||||
gcc -O2 -masm=intel -I../../include ./sched.c
|
gcc -O2 -Wall -Wextra -Wshadow -std=gnu11 -masm=intel -I../../include ./sched.c
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
// Desc: Scheduling algorithm //
|
// Desc: Scheduling algorithm //
|
||||||
//----------------------------------------------------------------------------//
|
//----------------------------------------------------------------------------//
|
||||||
|
|
||||||
#include <kernel/kernsched.h>
|
#include <kalkern.h>
|
||||||
|
|
||||||
#ifndef _KALEID_KERNEL
|
#ifndef _KALEID_KERNEL
|
||||||
|
|
||||||
|
@ -17,17 +17,18 @@ CREATE_PER_CPU(CurProc, Process_t *);
|
||||||
//
|
//
|
||||||
// For test purpose only
|
// For test purpose only
|
||||||
//
|
//
|
||||||
int procslen = 9;
|
int procslen = 10;
|
||||||
Process_t procs[] = {
|
Process_t procs[] = {
|
||||||
{ 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 4, 0, 0, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 4, 3, 3, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
{ 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
{ 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
|
{ 9, 2, 2, 21, 21, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -67,22 +68,22 @@ void SchedUnlock(void) {
|
||||||
// The four priority classes of OS/2
|
// The four priority classes of OS/2
|
||||||
//
|
//
|
||||||
|
|
||||||
CREATE_PER_CPU(IdlePrioProcs, ListHead_t *);
|
|
||||||
CREATE_PER_CPU(ReglPrioProcs, ListHead_t *);
|
|
||||||
CREATE_PER_CPU(ServPrioProcs, ListHead_t *);
|
|
||||||
CREATE_PER_CPU(TimeCritProcs, ListHead_t *);
|
CREATE_PER_CPU(TimeCritProcs, ListHead_t *);
|
||||||
|
CREATE_PER_CPU(ServPrioProcs, ListHead_t *);
|
||||||
|
CREATE_PER_CPU(ReglPrioProcs, ListHead_t *);
|
||||||
|
CREATE_PER_CPU(IdlePrioProcs, ListHead_t *);
|
||||||
|
|
||||||
const char *PrioClassesNames[] = {
|
const char *PrioClassesNames[] = {
|
||||||
"Idle priority class",
|
|
||||||
"Regular priority class",
|
|
||||||
"Server priority class",
|
|
||||||
"Time-critical class",
|
"Time-critical class",
|
||||||
|
"Server priority class",
|
||||||
|
"Regular priority class",
|
||||||
|
"Idle priority class",
|
||||||
};
|
};
|
||||||
|
|
||||||
enum { IDLE_PRIO_PROC = 0,
|
enum { TIME_CRIT_PROC = 0,
|
||||||
REGL_PRIO_PROC = 1,
|
SERV_PRIO_PROC = 1,
|
||||||
SERV_PRIO_PROC = 2,
|
REGL_PRIO_PROC = 2,
|
||||||
TIME_CRIT_PROC = 3,
|
IDLE_PRIO_PROC = 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -111,8 +112,8 @@ Process_t *CompareProcs(Process_t *proc1, Process_t *proc2)
|
||||||
{
|
{
|
||||||
KalAssert(proc1 && proc2);
|
KalAssert(proc1 && proc2);
|
||||||
|
|
||||||
if (proc1->prioClass > proc2->prioClass) return proc1;
|
if (proc1->prioClass < proc2->prioClass) return proc1;
|
||||||
if (proc1->prioClass < proc2->prioClass) return proc2;
|
if (proc1->prioClass > proc2->prioClass) return proc2;
|
||||||
|
|
||||||
if (proc1->prioLevel > proc2->prioLevel) return proc1;
|
if (proc1->prioLevel > proc2->prioLevel) return proc1;
|
||||||
if (proc1->prioLevel < proc2->prioLevel) return proc2;
|
if (proc1->prioLevel < proc2->prioLevel) return proc2;
|
||||||
|
@ -126,7 +127,7 @@ Process_t *CompareProcs(Process_t *proc1, Process_t *proc2)
|
||||||
static inline
|
static inline
|
||||||
void SchedThisProcUnlocked(Process_t *proc)
|
void SchedThisProcUnlocked(Process_t *proc)
|
||||||
{
|
{
|
||||||
KalAssert(proc && proc->procState == STATE_RUNNABLE);
|
KalAssert(proc && proc->procState == STATE_RUNNABLE && !proc->schedNode);
|
||||||
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
ListNode_t *iterNode = NULL;
|
ListNode_t *iterNode = NULL;
|
||||||
|
@ -137,13 +138,15 @@ void SchedThisProcUnlocked(Process_t *proc)
|
||||||
|
|
||||||
proc->schedNode = procNode;
|
proc->schedNode = procNode;
|
||||||
|
|
||||||
//printdbg("Adding process %d to '%s'\n", proc->pid, PrioClassesNames[proc->prioClass]);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Find a process with lesser priority
|
// Find a process with lesser priority
|
||||||
//
|
//
|
||||||
for (iterNode = head->first; iterNode; iterNode = iterNode->next) {
|
for (iterNode = head->first; iterNode; iterNode = iterNode->next) {
|
||||||
if (proc->prioLevel > GetNodeData(iterNode, Process_t *)->prioLevel) {
|
if (proc->prioLevel > GetNodeData(iterNode, Process_t *)->prioLevel) {
|
||||||
|
// Detect double insertions
|
||||||
|
KalAssert(proc->pid != GetNodeData(iterNode, Process_t *)->pid);
|
||||||
|
|
||||||
|
// Add process to schedule
|
||||||
AddNodeBefore(head, iterNode, procNode);
|
AddNodeBefore(head, iterNode, procNode);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
@ -198,9 +201,12 @@ void BlockCurProc(void)
|
||||||
|
|
||||||
ListNode_t *procNode = GetCurProc()->schedNode;
|
ListNode_t *procNode = GetCurProc()->schedNode;
|
||||||
|
|
||||||
|
KalAssert(procNode && "Blocking non-scheduled process");
|
||||||
|
|
||||||
GetCurProc()->procState = STATE_BLOCKED;
|
GetCurProc()->procState = STATE_BLOCKED;
|
||||||
RemoveNode(procNode->head, procNode);
|
RemoveNode(procNode->head, procNode);
|
||||||
|
|
||||||
|
GetCurProc()->schedNode = NULL;
|
||||||
SetCurProc(SelectSchedNext());
|
SetCurProc(SelectSchedNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,6 +214,7 @@ static inline
|
||||||
void ReSchedCurProc(void)
|
void ReSchedCurProc(void)
|
||||||
{
|
{
|
||||||
KalAssert(GetCurProc() && GetCurProc()->procState == STATE_RUNNING);
|
KalAssert(GetCurProc() && GetCurProc()->procState == STATE_RUNNING);
|
||||||
|
KalAssert(GetCurProc()->schedNode);
|
||||||
|
|
||||||
// Restore default attributes, cancelling boosts
|
// Restore default attributes, cancelling boosts
|
||||||
GetCurProc()->prioClass = GetCurProc()->defPrioClass;
|
GetCurProc()->prioClass = GetCurProc()->defPrioClass;
|
||||||
|
@ -301,7 +308,6 @@ leave:
|
||||||
void InitSched(void)
|
void InitSched(void)
|
||||||
{
|
{
|
||||||
int pid;
|
int pid;
|
||||||
Process_t *proc;
|
|
||||||
|
|
||||||
SchedLock();
|
SchedLock();
|
||||||
|
|
||||||
|
@ -345,7 +351,7 @@ void FiniSched(void)
|
||||||
|
|
||||||
#ifndef _KALEID_KERNEL
|
#ifndef _KALEID_KERNEL
|
||||||
|
|
||||||
#define PrintProc(proc) printdbg("{ %d, '%s', %d , %d}\n", (proc)->pid, \
|
#define PrintProc(proc) printdbg("{ %d, '%s', %d , %lu}\n", (proc)->pid, \
|
||||||
PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice);
|
PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -358,7 +364,7 @@ void PrintList(ListHead_t *head)
|
||||||
Process_t *proc;
|
Process_t *proc;
|
||||||
ListNode_t *node = head->first;
|
ListNode_t *node = head->first;
|
||||||
|
|
||||||
printdbg("len: %d\n", head->length);
|
printdbg("len: %lu\n", head->length);
|
||||||
|
|
||||||
while (node) {
|
while (node) {
|
||||||
proc = GetNodeData(node, Process_t *);
|
proc = GetNodeData(node, Process_t *);
|
||||||
|
@ -402,8 +408,8 @@ int main(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tick == 50) {
|
if (tick == 50) {
|
||||||
procs[2].procState = STATE_RUNNABLE;
|
procs[0].procState = STATE_RUNNABLE;
|
||||||
SchedThisProc(&procs[2]);
|
SchedThisProc(&procs[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Tick %d - Running: ", tick);
|
printf("Tick %d - Running: ", tick);
|
||||||
|
@ -418,6 +424,9 @@ int main(void)
|
||||||
|
|
||||||
SchedOnTick();
|
SchedOnTick();
|
||||||
|
|
||||||
|
if (tick == 50) // already done
|
||||||
|
puts("Re-scheduling process 0");
|
||||||
|
|
||||||
tick++;
|
tick++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue