This commit is contained in:
Imbus 2025-09-01 21:50:22 +02:00
parent ea016307cf
commit ae356930d8
8 changed files with 12 additions and 201 deletions

View file

@ -1,5 +1,5 @@
#pragma once
#include <types.h>
#include <stdint.h>
typedef struct {
volatile uint32_t v; // 0 = unlocked, 1 = locked

View file

@ -1,3 +1,6 @@
#include <config.h>
#include <spinlock.h>
#include <riscv.h>
#include <stdint.h>
typedef enum {
@ -37,6 +40,7 @@ struct Cpu {
int intena; // Were interrupts enabled before push_off()?
};
/** Saved registers for kernel context switches. */
typedef struct {
/* 0 */ uint64_t kernel_satp; // kernel page table
/* 8 */ uint64_t kernel_sp; // top of process's kernel stack
@ -75,3 +79,10 @@ typedef struct {
/* 272 */ uint64_t t5;
/* 280 */ uint64_t t6;
} TrapFrame_t;
struct Cpu *mycpu(void);
extern struct Cpu cpus[NCPU];
/** Per-process state */
struct Proc {};

View file

@ -1,21 +0,0 @@
#include <proc.h>
struct Cpu cpus[NCPU];
/**
* Must be called with interrupts disabled, to prevent race with process being
* moved to a different CPU.
*/
int cpuid() {
int id = read_tp();
return id;
}
/**
* Return this CPU's cpu struct. Interrupts must be disabled.
*/
struct Cpu *mycpu(void) {
int id = cpuid();
struct Cpu *c = &cpus[id];
return c;
}

View file

@ -1,22 +0,0 @@
#include <config.h>
#include <lib/spinlock.h>
#include <riscv.h>
#include <types.h>
struct Cpu *mycpu(void);
/** Saved registers for kernel context switches. */
struct Context {};
/** Per-CPU state. */
struct Cpu {
struct Proc *proc; // The process running on this cpu, or null.
struct Context context; // swtch() here to enter scheduler().
int noff; // Depth of push_off() nesting.
int intena; // Were interrupts enabled before push_off()?
};
extern struct Cpu cpus[NCPU];
/** Per-process state */
struct Proc {};

View file

@ -1,99 +0,0 @@
#include <string.h>
char *itoa(int value, char *str, int base) {
char *p = str;
char *p1, *p2;
unsigned int uvalue = value;
int negative = 0;
if (base < 2 || base > 36) {
*str = '\0';
return str;
}
if (value < 0 && base == 10) {
negative = 1;
uvalue = -value;
}
// Convert to string
do {
int digit = uvalue % base;
*p++ = (digit < 10) ? '0' + digit : 'a' + (digit - 10);
uvalue /= base;
} while (uvalue);
if (negative)
*p++ = '-';
*p = '\0';
// Reverse string
p1 = str;
p2 = p - 1;
while (p1 < p2) {
char tmp = *p1;
*p1++ = *p2;
*p2-- = tmp;
}
return str;
}
void *memset(void *dst, int c, size_t length) {
u8 *ptr = (u8 *)dst;
const u8 value = (u8)c;
while (length--) *(ptr++) = value;
return dst;
}
void *memcpy(void *dst, const void *src, size_t len) {
u8 *d = (u8 *)dst;
const u8 *s = (const u8 *)src;
for (size_t i = 0; i < len; i++) {
d[i] = s[i];
}
return dst;
}
void *memmove(void *dst, const void *src, size_t len) {
u8 *d = (u8 *)dst;
const u8 *s = (const u8 *)src;
if (d < s) {
for (size_t i = 0; i < len; i++) {
d[i] = s[i];
}
} else if (d > s) {
for (size_t i = len; i > 0; i--) {
d[i - 1] = s[i - 1];
}
}
return dst;
}
int memcmp(const void *s1, const void *s2, size_t len) {
const u8 *a = (const u8 *)s1;
const u8 *b = (const u8 *)s2;
for (size_t i = 0; i < len; i++) {
if (a[i] != b[i]) {
return (int)a[i] - (int)b[i];
}
}
return 0;
}
size_t strlen(const char *s) {
const char *p = s;
while (*p) ++p;
return (size_t)(p - s);
}
size_t strnlen(const char *s, size_t maxlen) {
size_t len = 0;
while (len < maxlen && s[len] != '\0') {
len++;
}
return len;
}

View file

@ -1,40 +0,0 @@
#ifndef KERNEL_STRING_H
#define KERNEL_STRING_H
#include <types.h>
/** Integer to ascii */
char *itoa(int value, char *str, int base);
/** Fill memory with constant byte */
void *memset(void *dst, int c, size_t len);
/** Copy `len` bytes from `src` to `dst`. Undefined if regions overlap. */
void *memcpy(void *dst, const void *src, size_t len);
/** Copy `len` bytes from `src` to `dst`, safe for overlapping regions. */
void *memmove(void *dst, const void *src, size_t len);
/** Compare `len` bytes of `s1` and `s2`.
* Returns 0 if equal, <0 if s1 < s2, >0 if s1 > s2. */
int memcmp(const void *s1, const void *s2, size_t len);
/** Returns the length of a null-terminated string */
size_t strlen(const char *s);
/** Return length of string `s`, up to a max of `maxlen` bytes */
size_t strnlen(const char *s, size_t maxlen);
// TODO: These:
/*
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strcpy(char *dst, const char *src);
char *strncpy(char *dst, const char *src, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
*/
#endif

View file

@ -1,8 +0,0 @@
/* QEMU memory maps a UART device here. */
#define UART_BASE ((volatile char *)0x10000000)
void uart_putc(char c) { *UART_BASE = c; }
void uart_puts(const char *s) {
while (*s) uart_putc(*s++);
}

View file

@ -1,10 +0,0 @@
#ifndef UART_KERNEL_H
#define UART_KERNEL_H
/** Send a single character to the UART device */
void uart_putc(char c);
/** Send a **NULL TERMINATED** string to the UART device */
void uart_puts(const char *s);
#endif