Compare commits
13 Commits
01a1e0aaa9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 090e97e3e1 | |||
| b9b8d1147c | |||
| ff788bb819 | |||
| 172da6cf07 | |||
| 94636f8c09 | |||
| 889a16d168 | |||
| 5cd7aa3cab | |||
| 968b26fd93 | |||
| 2abfbec7e6 | |||
| 5a490b03f9 | |||
| c4596dcd16 | |||
| 3496194425 | |||
| 0aa03bf92a |
@@ -8,7 +8,6 @@
|
||||
#define va_end __builtin_va_end
|
||||
#define va_arg __builtin_va_arg
|
||||
|
||||
void r_printf(const char *fmt, ...);
|
||||
size_t r_strlen(const char *str);
|
||||
void kprintf(const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#ifndef RIVOS_KERNEL_H
|
||||
#define RIVOS_KERNEL_H
|
||||
|
||||
typedef struct sbiret_s {
|
||||
typedef struct sbiret {
|
||||
long error;
|
||||
long value;
|
||||
} sbiret_s;
|
||||
|
||||
void kputchar(char ch);
|
||||
|
||||
#endif
|
||||
|
||||
10
include/memory.h
Normal file
10
include/memory.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#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
|
||||
15
include/string.h
Normal file
15
include/string.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#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
|
||||
@@ -5,7 +5,13 @@
|
||||
* 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
4
run.sh
@@ -18,7 +18,9 @@ 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/io.c \
|
||||
src/memory.c \
|
||||
src/string.c
|
||||
|
||||
# Run QEMU
|
||||
# -machine virt: generic riscv platform
|
||||
|
||||
37
src/io.c
37
src/io.c
@@ -1,14 +1,15 @@
|
||||
#include "../include/io.h"
|
||||
#include "../include/string.h"
|
||||
|
||||
void k_putchar(char ch);
|
||||
void kputchar(char ch);
|
||||
|
||||
void r_printf(const char *fmt, ...) {
|
||||
void kprintf(const char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
while (*fmt) {
|
||||
if (*fmt != '%') {
|
||||
k_putchar(*fmt);
|
||||
kputchar(*fmt);
|
||||
fmt++;
|
||||
continue;
|
||||
}
|
||||
@@ -18,13 +19,13 @@ void r_printf(const char *fmt, ...) {
|
||||
switch (*fmt) {
|
||||
case '\0': {
|
||||
/* Simply print the char */
|
||||
k_putchar('%');
|
||||
kputchar('%');
|
||||
|
||||
goto end;
|
||||
}
|
||||
case '%': {
|
||||
/* Print '%' */
|
||||
k_putchar('%');
|
||||
kputchar('%');
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -32,7 +33,7 @@ void r_printf(const char *fmt, ...) {
|
||||
/* Print a NULL-terminated string */
|
||||
const char *s = va_arg(args, const char *);
|
||||
while (*s) {
|
||||
k_putchar(*s);
|
||||
kputchar(*s);
|
||||
s++;
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ void r_printf(const char *fmt, ...) {
|
||||
int value = va_arg(args, int);
|
||||
unsigned magnitude = value;
|
||||
if (value < 0) {
|
||||
k_putchar('-');
|
||||
kputchar('-');
|
||||
magnitude = -magnitude;
|
||||
}
|
||||
|
||||
@@ -53,12 +54,9 @@ void r_printf(const char *fmt, ...) {
|
||||
}
|
||||
|
||||
while (divisor > 0) {
|
||||
k_putchar('0' + magnitude);
|
||||
k_putchar('0' + magnitude / divisor);
|
||||
kputchar('0' + magnitude / divisor);
|
||||
magnitude %= divisor;
|
||||
k_putchar('0' + magnitude);
|
||||
divisor /= 10;
|
||||
k_putchar('0' + magnitude);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -69,14 +67,14 @@ void r_printf(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 */
|
||||
k_putchar("0123456789ABCDEF"[nibble]);
|
||||
kputchar("0123456789ABCDEF"[nibble]);
|
||||
}
|
||||
}
|
||||
default: {
|
||||
const char *str = "NOT IMPLEMENTED";
|
||||
size_t len = r_strlen(str);
|
||||
size_t len = kstrlen(str);
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
k_putchar(str[i]);
|
||||
kputchar(str[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,14 +85,3 @@ void r_printf(const char *fmt, ...) {
|
||||
end:
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
size_t r_strlen(const char *str) {
|
||||
size_t len = 0;
|
||||
|
||||
while (*str) {
|
||||
len++;
|
||||
str++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
21
src/kernel.c
21
src/kernel.c
@@ -1,6 +1,6 @@
|
||||
#include "../include/kernel.h"
|
||||
#include "../include/io.h"
|
||||
#include "../include/types.h"
|
||||
#include "../include/memory.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,27 +31,18 @@ sbiret_s sbi_call(long arg0, long arg1, long arg2, long arg3, long arg4, long ar
|
||||
return (sbiret_s){.error = a0, .value = a1};
|
||||
}
|
||||
|
||||
void k_putchar(char ch) {
|
||||
void kputchar(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 */
|
||||
k_memset(__bss, 0, __bss_end - __bss);
|
||||
kmemset(__bss, 0, __bss_end - __bss);
|
||||
|
||||
/* Start here */
|
||||
r_printf("%s, %s\n", "Hello", "world!");
|
||||
r_printf("3 + 4 = %d\n", 3 + 4);
|
||||
kprintf("%s, %s\n", "Hello", "world!");
|
||||
kprintf("3 + 4 = %d\n", 3 + 4);
|
||||
|
||||
for (;;) {
|
||||
__asm__ __volatile__("wfi");
|
||||
|
||||
21
src/memory.c
Normal file
21
src/memory.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#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;
|
||||
}
|
||||
29
src/string.c
Normal file
29
src/string.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user