Mass reformat

This commit is contained in:
Imbus 2025-09-01 22:43:50 +02:00
parent 3450ab2cb8
commit f1fd7de79f
10 changed files with 317 additions and 297 deletions

View file

@ -1,10 +1,10 @@
#include <spinlock.h>
#include <kalloc.h> #include <kalloc.h>
#include <memory.h> #include <memory.h>
#include <panic.h> #include <panic.h>
#include <riscv.h> #include <riscv.h>
#include <string.h> #include <spinlock.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
// Physical memory allocator, for user processes, // Physical memory allocator, for user processes,
// kernel stacks, page-table pages, // kernel stacks, page-table pages,

View file

@ -52,24 +52,12 @@ int kvsnprintf(char *buf, size_t size, const char *fmt, va_list args) {
fmt++; // skip '%' fmt++; // skip '%'
switch (*fmt) { switch (*fmt) {
case 's': case 's': append_str(&p, &remaining, va_arg(args, const char *)); break;
append_str(&p, &remaining, va_arg(args, const char *)); case 'd': append_int(&p, &remaining, va_arg(args, int), 10); break;
break; case 'x': append_int(&p, &remaining, va_arg(args, unsigned int), 16); break;
case 'd': case 'c': append_char(&p, &remaining, (char)va_arg(args, int)); break;
append_int(&p, &remaining, va_arg(args, int), 10); case '%': append_char(&p, &remaining, '%'); break;
break; default: append_char(&p, &remaining, '?'); break;
case 'x':
append_int(&p, &remaining, va_arg(args, unsigned int), 16);
break;
case 'c':
append_char(&p, &remaining, (char)va_arg(args, int));
break;
case '%':
append_char(&p, &remaining, '%');
break;
default:
append_char(&p, &remaining, '?');
break;
} }
fmt++; fmt++;
} }

View file

@ -4,88 +4,133 @@
#include <types.h> #include <types.h>
/** Swap byte order of 16-bit value */ /** Swap byte order of 16-bit value */
static inline u16 swap16(u16 x) { return (x >> 8) | (x << 8); } static inline u16 swap16(u16 x) {
return (x >> 8) | (x << 8);
}
/** Swap byte order of 32-bit value */ /** Swap byte order of 32-bit value */
static inline u32 swap32(u32 x) { static inline u32 swap32(u32 x) {
return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) | return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) | ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
} }
/** Swap byte order of 64-bit value */ /** Swap byte order of 64-bit value */
static inline u64 swap64(u64 x) { static inline u64 swap64(u64 x) {
return ((x >> 56) & 0x00000000000000ffULL) | return ((x >> 56) & 0x00000000000000ffULL) | ((x >> 40) & 0x000000000000ff00ULL) |
((x >> 40) & 0x000000000000ff00ULL) | ((x >> 24) & 0x0000000000ff0000ULL) | ((x >> 8) & 0x00000000ff000000ULL) |
((x >> 24) & 0x0000000000ff0000ULL) | ((x << 8) & 0x000000ff00000000ULL) | ((x << 24) & 0x0000ff0000000000ULL) |
((x >> 8) & 0x00000000ff000000ULL) | ((x << 40) & 0x00ff000000000000ULL) | ((x << 56) & 0xff00000000000000ULL);
((x << 8) & 0x000000ff00000000ULL) |
((x << 24) & 0x0000ff0000000000ULL) |
((x << 40) & 0x00ff000000000000ULL) |
((x << 56) & 0xff00000000000000ULL);
} }
#ifdef __LITTLE_ENDIAN__ #ifdef __LITTLE_ENDIAN__
/** Convert 16-bit value to little-endian */ /** Convert 16-bit value to little-endian */
static inline u16 to_le16(u16 x) { return x; } static inline u16 to_le16(u16 x) {
return x;
}
/** Convert 16-bit little-endian value to host */ /** Convert 16-bit little-endian value to host */
static inline u16 from_le16(u16 x) { return x; } static inline u16 from_le16(u16 x) {
return x;
}
/** Convert 32-bit value to little-endian */ /** Convert 32-bit value to little-endian */
static inline u32 to_le32(u32 x) { return x; } static inline u32 to_le32(u32 x) {
return x;
}
/** Convert 32-bit little-endian value to host */ /** Convert 32-bit little-endian value to host */
static inline u32 from_le32(u32 x) { return x; } static inline u32 from_le32(u32 x) {
return x;
}
/** Convert 64-bit value to little-endian */ /** Convert 64-bit value to little-endian */
static inline u64 to_le64(u64 x) { return x; } static inline u64 to_le64(u64 x) {
return x;
}
/** Convert 64-bit little-endian value to host */ /** Convert 64-bit little-endian value to host */
static inline u64 from_le64(u64 x) { return x; } static inline u64 from_le64(u64 x) {
return x;
}
/** Convert 16-bit value to big-endian */ /** Convert 16-bit value to big-endian */
static inline u16 to_be16(u16 x) { return swap16(x); } static inline u16 to_be16(u16 x) {
return swap16(x);
}
/** Convert 16-bit big-endian value to host */ /** Convert 16-bit big-endian value to host */
static inline u16 from_be16(u16 x) { return swap16(x); } static inline u16 from_be16(u16 x) {
return swap16(x);
}
/** Convert 32-bit value to big-endian */ /** Convert 32-bit value to big-endian */
static inline u32 to_be32(u32 x) { return swap32(x); } static inline u32 to_be32(u32 x) {
return swap32(x);
}
/** Convert 32-bit big-endian value to host */ /** Convert 32-bit big-endian value to host */
static inline u32 from_be32(u32 x) { return swap32(x); } static inline u32 from_be32(u32 x) {
return swap32(x);
}
/** Convert 64-bit value to big-endian */ /** Convert 64-bit value to big-endian */
static inline u64 to_be64(u64 x) { return swap64(x); } static inline u64 to_be64(u64 x) {
return swap64(x);
}
/** Convert 64-bit big-endian value to host */ /** Convert 64-bit big-endian value to host */
static inline u64 from_be64(u64 x) { return swap64(x); } static inline u64 from_be64(u64 x) {
return swap64(x);
}
#else // Big-endian #else // Big-endian
/** Convert 16-bit value to little-endian */ /** Convert 16-bit value to little-endian */
static inline u16 to_le16(u16 x) { return swap16(x); } static inline u16 to_le16(u16 x) {
return swap16(x);
}
/** Convert 16-bit little-endian value to host */ /** Convert 16-bit little-endian value to host */
static inline u16 from_le16(u16 x) { return swap16(x); } static inline u16 from_le16(u16 x) {
return swap16(x);
}
/** Convert 32-bit value to little-endian */ /** Convert 32-bit value to little-endian */
static inline u32 to_le32(u32 x) { return swap32(x); } static inline u32 to_le32(u32 x) {
return swap32(x);
}
/** Convert 32-bit little-endian value to host */ /** Convert 32-bit little-endian value to host */
static inline u32 from_le32(u32 x) { return swap32(x); } static inline u32 from_le32(u32 x) {
return swap32(x);
}
/** Convert 64-bit value to little-endian */ /** Convert 64-bit value to little-endian */
static inline u64 to_le64(u64 x) { return swap64(x); } static inline u64 to_le64(u64 x) {
return swap64(x);
}
/** Convert 64-bit little-endian value to host */ /** Convert 64-bit little-endian value to host */
static inline u64 from_le64(u64 x) { return swap64(x); } static inline u64 from_le64(u64 x) {
return swap64(x);
}
/** Convert 16-bit value to big-endian */ /** Convert 16-bit value to big-endian */
static inline u16 to_be16(u16 x) { return x; } static inline u16 to_be16(u16 x) {
return x;
}
/** Convert 16-bit big-endian value to host */ /** Convert 16-bit big-endian value to host */
static inline u16 from_be16(u16 x) { return x; } static inline u16 from_be16(u16 x) {
return x;
}
/** Convert 32-bit value to big-endian */ /** Convert 32-bit value to big-endian */
static inline u32 to_be32(u32 x) { return x; } static inline u32 to_be32(u32 x) {
return x;
}
/** Convert 32-bit big-endian value to host */ /** Convert 32-bit big-endian value to host */
static inline u32 from_be32(u32 x) { return x; } static inline u32 from_be32(u32 x) {
return x;
}
/** Convert 64-bit value to big-endian */ /** Convert 64-bit value to big-endian */
static inline u64 to_be64(u64 x) { return x; } static inline u64 to_be64(u64 x) {
return x;
}
/** Convert 64-bit big-endian value to host */ /** Convert 64-bit big-endian value to host */
static inline u64 from_be64(u64 x) { return x; } static inline u64 from_be64(u64 x) {
return x;
}
#endif // __LITTLE_ENDIAN__ #endif // __LITTLE_ENDIAN__

View file

@ -10,8 +10,7 @@ size_t probe_memory(void) {
u32 test_pattern = 0xA5A5A5A5; u32 test_pattern = 0xA5A5A5A5;
size_t detected = 0; size_t detected = 0;
for (size_t offset = 4096 * 16; offset < MAX_PROBE_SIZE; for (size_t offset = 4096 * 16; offset < MAX_PROBE_SIZE; offset += PROBE_STEP) {
offset += PROBE_STEP) {
addr = (volatile u32 *)(KERNBASE + offset); addr = (volatile u32 *)(KERNBASE + offset);
u32 old = *addr; u32 old = *addr;

View file

@ -43,18 +43,13 @@
#include "mini-printf.h" #include "mini-printf.h"
static int static int mini_strlen(const char *s) {
mini_strlen(const char *s)
{
int len = 0; int len = 0;
while (s[len] != '\0') len++; while (s[len] != '\0') len++;
return len; return len;
} }
static int static int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *buffer) {
mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
char *buffer)
{
char *pbuffer = buffer; char *pbuffer = buffer;
int negative = 0; int negative = 0;
int i, len; int i, len;
@ -92,13 +87,12 @@ mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
return len; return len;
} }
static int static int mini_pad(char *ptr, int len, char pad_char, int pad_to, char *buffer) {
mini_pad(char* ptr, int len, char pad_char, int pad_to, char *buffer)
{
int i; int i;
int overflow = 0; int overflow = 0;
char *pbuffer = buffer; char *pbuffer = buffer;
if(pad_to == 0) pad_to = len; if (pad_to == 0)
pad_to = len;
if (len > pad_to) { if (len > pad_to) {
len = pad_to; len = pad_to;
overflow = 1; overflow = 1;
@ -123,10 +117,9 @@ struct mini_buff {
unsigned int buffer_len; unsigned int buffer_len;
}; };
static int static int _puts(char *s, int len, void *buf) {
_puts(char *s, int len, void *buf) if (!buf)
{ return len;
if(!buf) return len;
struct mini_buff *b = buf; struct mini_buff *b = buf;
char *p0 = b->buffer; char *p0 = b->buffer;
int i; int i;
@ -146,25 +139,21 @@ static int (*mini_handler) (void* data, void* obj, int ch, int lhint, char** bf)
static void (*mini_handler_freeor)(void *data, void *) = 0; static void (*mini_handler_freeor)(void *data, void *) = 0;
static void *mini_handler_data = 0; static void *mini_handler_data = 0;
void mini_printf_set_handler( void mini_printf_set_handler(void *data, int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf),
void* data, void (*freeor)(void *data, void *buf)) {
int (*handler)(void* data, void* obj, int ch, int len_hint, char** buf),
void (*freeor)(void* data, void* buf))
{
mini_handler = handler; mini_handler = handler;
mini_handler_freeor = freeor; mini_handler_freeor = freeor;
mini_handler_data = data; mini_handler_data = data;
} }
#endif #endif
int int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va) {
mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
{
struct mini_buff b; struct mini_buff b;
b.buffer = buffer; b.buffer = buffer;
b.pbuffer = buffer; b.pbuffer = buffer;
b.buffer_len = buffer_len; b.buffer_len = buffer_len;
if(buffer_len == 0) buffer = (void*) 0; if (buffer_len == 0)
buffer = (void *)0;
int n = mini_vpprintf(_puts, (buffer != (void *)0) ? &b : (void *)0, fmt, va); int n = mini_vpprintf(_puts, (buffer != (void *)0) ? &b : (void *)0, fmt, va);
if (buffer == (void *)0) { if (buffer == (void *)0) {
return n; return n;
@ -172,9 +161,7 @@ mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list v
return b.pbuffer - b.buffer; return b.pbuffer - b.buffer;
} }
int int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va) {
mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va)
{
char bf[24]; char bf[24];
char bf2[24]; char bf2[24];
char ch; char ch;
@ -183,7 +170,8 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
#endif #endif
if (puts == (void *)0) { if (puts == (void *)0) {
/* run puts in counting mode. */ /* run puts in counting mode. */
puts = _puts; buf = (void*)0; puts = _puts;
buf = (void *)0;
} }
int n = 0; int n = 0;
while ((ch = *(fmt++))) { while ((ch = *(fmt++))) {
@ -200,7 +188,8 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
ch = *(fmt++); ch = *(fmt++);
/* Zero padding requested */ /* Zero padding requested */
if (ch == '0') pad_char = '0'; if (ch == '0')
pad_char = '0';
while (ch >= '0' && ch <= '9') { while (ch >= '0' && ch <= '9') {
pad_to = pad_to * 10 + (ch - '0'); pad_to = pad_to * 10 + (ch - '0');
ch = *(fmt++); ch = *(fmt++);
@ -214,8 +203,7 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
} }
switch (ch) { switch (ch) {
case 0: case 0: goto end;
goto end;
case 'u': case 'u':
case 'd': case 'd':
if (l) { if (l) {
@ -284,10 +272,7 @@ end:
return n; return n;
} }
int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...) {
int
mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
{
int ret; int ret;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -297,9 +282,7 @@ mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
return ret; return ret;
} }
int int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...) {
mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...)
{
int ret; int ret;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -308,4 +291,3 @@ mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt
return ret; return ret;
} }

View file

@ -27,7 +27,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef __MINI_PRINTF__ #ifndef __MINI_PRINTF__
#define __MINI_PRINTF__ #define __MINI_PRINTF__
@ -42,8 +41,7 @@ extern "C" {
* void* arguments matching %O and %R are sent to handler as obj. * void* arguments matching %O and %R are sent to handler as obj.
* the result string created by handler at *buf is freed by freeor. * the result string created by handler at *buf is freed by freeor.
* */ * */
void mini_printf_set_handler( void mini_printf_set_handler(void *data,
void * data,
/* handler returns number of chars in *buf; *buf is not NUL-terminated. */ /* handler returns number of chars in *buf; *buf is not NUL-terminated. */
int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf), int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf),
void (*freeor)(void *data, void *buf)); void (*freeor)(void *data, void *buf));

View file

@ -39,7 +39,9 @@ static inline u64 read_tp() {
} }
/** Write thread pointer */ /** Write thread pointer */
static inline void write_tp(u64 x) { asm volatile("mv tp, %0" : : "r"(x)); } static inline void write_tp(u64 x) {
asm volatile("mv tp, %0" : : "r"(x));
}
/** /**
* Read the value of the sstatus register. * Read the value of the sstatus register.
@ -60,10 +62,14 @@ static inline void w_sstatus(u64 x) {
} }
/** Enable device interrupts */ /** Enable device interrupts */
static inline void intr_on() { w_sstatus(r_sstatus() | SSTATUS_SIE); } static inline void intr_on() {
w_sstatus(r_sstatus() | SSTATUS_SIE);
}
/** Disable device interrupts */ /** Disable device interrupts */
static inline void intr_off() { w_sstatus(r_sstatus() & ~SSTATUS_SIE); } static inline void intr_off() {
w_sstatus(r_sstatus() & ~SSTATUS_SIE);
}
/** Are device interrupts enabled? */ /** Are device interrupts enabled? */
static inline int intr_get() { static inline int intr_get() {

View file

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

View file

@ -1,6 +1,6 @@
#include <config.h> #include <config.h>
#include <spinlock.h>
#include <riscv.h> #include <riscv.h>
#include <spinlock.h>
#include <stdint.h> #include <stdint.h>
typedef enum { typedef enum {

View file

@ -1,9 +1,9 @@
#include <config.h> #include <config.h>
#include <spinlock.h>
#include <kalloc.h> #include <kalloc.h>
#include <memory.h> #include <memory.h>
#include <proc.h> #include <proc.h>
#include <riscv.h> #include <riscv.h>
#include <spinlock.h>
#include <stdint.h> #include <stdint.h>
#include <uart.h> #include <uart.h>