Compare commits

..

9 Commits

11 changed files with 45 additions and 109 deletions

View File

@@ -1,5 +1,3 @@
# rivos
A small educational operating system targeting the RISC-V architecture.
Focused on simplicity, low-level exploration, and learning by building.
This toy os is made following the [OS in 1,000 lines](https://1000os.seiya.me/en/) project.

View File

@@ -8,6 +8,7 @@
#define va_end __builtin_va_end
#define va_arg __builtin_va_arg
void kprintf(const char *fmt, ...);
void r_printf(const char *fmt, ...);
size_t r_strlen(const char *str);
#endif

View File

@@ -1,11 +1,9 @@
#ifndef RIVOS_KERNEL_H
#define RIVOS_KERNEL_H
typedef struct sbiret {
typedef struct sbiret_s {
long error;
long value;
} sbiret_s;
void kputchar(char ch);
#endif

View File

@@ -1,10 +0,0 @@
#ifndef RIVOS_MEMORY_H
#define RIVOS_MEMORY_H
#include "types.h"
void *kmemset(void *buf, char c, size_t n);
void *kmemcpy(void *dst, const void *src, size_t n);
#endif

View File

@@ -1,15 +0,0 @@
#ifndef RIVOS_STRING_H
#define RIVOS_STRING_H
#include "types.h"
/**
* This is not a safe function, check dst size
*/
char *kstrcpy(char *dst, const char *src);
size_t kstrlen(const char *str);
int kstrcmp(const char *s1, const char *s2);
#endif

View File

@@ -5,13 +5,7 @@
* Define our standard types
*/
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef u32 size_t;
#define true 1
#define false 0
#define NULL ((void *)0)
#endif

4
run.sh
View File

@@ -18,9 +18,7 @@ CFLAGS="-std=c11 -O2 -g3 -Wall -Wextra --target=riscv32-unknown-elf -fuse-ld=lld
$CC $CFLAGS -Wl,-Tkernel.ld -Wl,-Map=kernel.map -o kernel.elf \
src/kernel.c \
src/io.c \
src/memory.c \
src/string.c
src/io.c
# Run QEMU
# -machine virt: generic riscv platform

View File

@@ -1,15 +1,14 @@
#include "../include/io.h"
#include "../include/string.h"
void kputchar(char ch);
void k_putchar(char ch);
void kprintf(const char *fmt, ...) {
void r_printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
while (*fmt) {
if (*fmt != '%') {
kputchar(*fmt);
k_putchar(*fmt);
fmt++;
continue;
}
@@ -19,13 +18,13 @@ void kprintf(const char *fmt, ...) {
switch (*fmt) {
case '\0': {
/* Simply print the char */
kputchar('%');
k_putchar('%');
goto end;
}
case '%': {
/* Print '%' */
kputchar('%');
k_putchar('%');
break;
}
@@ -33,7 +32,7 @@ void kprintf(const char *fmt, ...) {
/* Print a NULL-terminated string */
const char *s = va_arg(args, const char *);
while (*s) {
kputchar(*s);
k_putchar(*s);
s++;
}
@@ -44,7 +43,7 @@ void kprintf(const char *fmt, ...) {
int value = va_arg(args, int);
unsigned magnitude = value;
if (value < 0) {
kputchar('-');
k_putchar('-');
magnitude = -magnitude;
}
@@ -54,9 +53,12 @@ void kprintf(const char *fmt, ...) {
}
while (divisor > 0) {
kputchar('0' + magnitude / divisor);
k_putchar('0' + magnitude);
k_putchar('0' + magnitude / divisor);
magnitude %= divisor;
k_putchar('0' + magnitude);
divisor /= 10;
k_putchar('0' + magnitude);
}
break;
@@ -67,14 +69,14 @@ void kprintf(const char *fmt, ...) {
for (int i = 7; i >= 0; --i) {
unsigned nibble = (value >> (i * 4)) & 0xF;
/* From a char array we get the nibble position */
kputchar("0123456789ABCDEF"[nibble]);
k_putchar("0123456789ABCDEF"[nibble]);
}
}
default: {
const char *str = "NOT IMPLEMENTED";
size_t len = kstrlen(str);
size_t len = r_strlen(str);
for (size_t i = 0; i < len; ++i) {
kputchar(str[i]);
k_putchar(str[i]);
}
}
}
@@ -85,3 +87,14 @@ void kprintf(const char *fmt, ...) {
end:
va_end(args);
}
size_t r_strlen(const char *str) {
size_t len = 0;
while (*str) {
len++;
str++;
}
return len;
}

View File

@@ -1,6 +1,6 @@
#include "../include/kernel.h"
#include "../include/io.h"
#include "../include/memory.h"
#include "../include/types.h"
extern char __bss[], __bss_end[], __stack_top[];
@@ -14,7 +14,7 @@ extern char __bss[], __bss_end[], __stack_top[];
* a0 returns and error code
*/
sbiret_s sbi_call(long arg0, long arg1, long arg2, long arg3, long arg4, long arg5, long fid, long eid) {
register long a0 __asm__("a0") = arg0;
register long a0 __asm("a0") = arg0;
register long a1 __asm__("a1") = arg1;
register long a2 __asm__("a2") = arg2;
register long a3 __asm__("a3") = arg3;
@@ -31,18 +31,27 @@ sbiret_s sbi_call(long arg0, long arg1, long arg2, long arg3, long arg4, long ar
return (sbiret_s){.error = a0, .value = a1};
}
void kputchar(char ch) {
void k_putchar(char ch) {
sbi_call(ch, 0, 0, 0, 0, 0, 0, 1);
}
void *k_memset(void *buf, char c, size_t n) {
u8 *p = (u8 *)buf;
while (n--) {
*p++ = c;
}
return buf;
}
/* Kernel main */
void kernel_main(void) {
/* Usually it is not required */
kmemset(__bss, 0, __bss_end - __bss);
k_memset(__bss, 0, __bss_end - __bss);
/* Start here */
kprintf("%s, %s\n", "Hello", "world!");
kprintf("3 + 4 = %d\n", 3 + 4);
r_printf("%s, %s\n", "Hello", "world!");
r_printf("3 + 4 = %d\n", 3 + 4);
for (;;) {
__asm__ __volatile__("wfi");

View File

@@ -1,21 +0,0 @@
#include "../include/memory.h"
void *kmemset(void *buf, char c, size_t n) {
u8 *p = (u8 *)buf;
while (n--) {
*p++ = c;
}
return buf;
}
void *kmemcpy(void *dst, const void *src, size_t n) {
u8 *dest = (u8 *)dst;
const u8 *source = (u8 *)src;
while (n--) {
*dest++ = *source++;
}
return dst;
}

View File

@@ -1,29 +0,0 @@
#include "../include/string.h"
char *kstrcpy(char *dst, const char *src) {
while (*src) {
*dst = *src;
src++;
}
return dst;
}
size_t kstrlen(const char *str) {
size_t len = 0;
while (*str) {
len++;
str++;
}
return len;
}
int kstrcmp(const char *s1, const char *s2) {
/**
* TODO
*/
return 0;
}