feat(memory, string): add memory and string utilities

This commit is contained in:
2026-01-26 15:22:46 +01:00
parent b9b8d1147c
commit 090e97e3e1
6 changed files with 84 additions and 16 deletions

10
include/memory.h Normal file
View 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
View 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

4
run.sh
View File

@@ -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 \ $CC $CFLAGS -Wl,-Tkernel.ld -Wl,-Map=kernel.map -o kernel.elf \
src/kernel.c \ src/kernel.c \
src/io.c src/io.c \
src/memory.c \
src/string.c
# Run QEMU # Run QEMU
# -machine virt: generic riscv platform # -machine virt: generic riscv platform

View File

@@ -1,6 +1,6 @@
#include "../include/kernel.h" #include "../include/kernel.h"
#include "../include/io.h" #include "../include/io.h"
#include "../include/types.h" #include "../include/memory.h"
extern char __bss[], __bss_end[], __stack_top[]; extern char __bss[], __bss_end[], __stack_top[];
@@ -14,7 +14,7 @@ extern char __bss[], __bss_end[], __stack_top[];
* a0 returns and error code * 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) { 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 a1 __asm__("a1") = arg1;
register long a2 __asm__("a2") = arg2; register long a2 __asm__("a2") = arg2;
register long a3 __asm__("a3") = arg3; 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}; 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); 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 */ /* Kernel main */
void kernel_main(void) { void kernel_main(void) {
/* Usually it is not required */ /* Usually it is not required */
k_memset(__bss, 0, __bss_end - __bss); kmemset(__bss, 0, __bss_end - __bss);
/* Start here */ /* Start here */
r_printf("%s, %s\n", "Hello", "world!"); kprintf("%s, %s\n", "Hello", "world!");
r_printf("3 + 4 = %d\n", 3 + 4); kprintf("3 + 4 = %d\n", 3 + 4);
for (;;) { for (;;) {
__asm__ __volatile__("wfi"); __asm__ __volatile__("wfi");

21
src/memory.c Normal file
View 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
View 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;
}