Hello world over serial
This commit is contained in:
@@ -5,10 +5,12 @@ ldflags :=
|
||||
cflags :=
|
||||
buildtype ?= release
|
||||
|
||||
include $(platform)/src.mk
|
||||
include $(platform)/flags.mk
|
||||
include generic/flags.mk
|
||||
|
||||
include $(platform)/src.mk
|
||||
include libk/src.mk
|
||||
|
||||
all: build/kernel.elf
|
||||
|
||||
build/kernel.elf: $(o)
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#include <limine/limine.h>
|
||||
#include <amd64/init.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
void bootmain(void) {
|
||||
amd64_init();
|
||||
|
||||
DEBUG("Hello from amd64!\n");
|
||||
|
||||
for (;;);
|
||||
}
|
||||
|
||||
46
kernel/amd64/debug.c
Normal file
46
kernel/amd64/debug.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <libk/printf.h>
|
||||
#include <sys/debug.h>
|
||||
#include <amd64/debug.h>
|
||||
#include <amd64/io.h>
|
||||
|
||||
#define PORT_COM1 0x03F8
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
static bool amd64_debug_serial_tx_empty(void) {
|
||||
return (bool)(amd64_io_inb(PORT_COM1 + 5) & 0x20);
|
||||
}
|
||||
|
||||
static void amd64_debug_serial_write(char x) {
|
||||
while (!amd64_debug_serial_tx_empty());
|
||||
amd64_io_outb(PORT_COM1, (uint8_t)x);
|
||||
}
|
||||
|
||||
void debugprintf(const char *fmt, ...) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buffer, sizeof(buffer), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
buffer[sizeof(buffer) - 1] = '\0';
|
||||
|
||||
const char *p = buffer;
|
||||
while (*p) {
|
||||
amd64_debug_serial_write(*p);
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
void amd64_debug_init(void) {
|
||||
amd64_io_outb(PORT_COM1 + 1, 0x00);
|
||||
amd64_io_outb(PORT_COM1 + 3, 0x80);
|
||||
amd64_io_outb(PORT_COM1 + 0, 0x03);
|
||||
amd64_io_outb(PORT_COM1 + 1, 0x00);
|
||||
amd64_io_outb(PORT_COM1 + 3, 0x03);
|
||||
amd64_io_outb(PORT_COM1 + 2, 0xC7);
|
||||
amd64_io_outb(PORT_COM1 + 4, 0x0B);
|
||||
}
|
||||
6
kernel/amd64/debug.h
Normal file
6
kernel/amd64/debug.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef _KERNEL_AMD64_DEBUG_H
|
||||
#define _KERNEL_AMD64_DEBUG_H
|
||||
|
||||
void amd64_debug_init(void);
|
||||
|
||||
#endif // _KERNEL_AMD64_DEBUG_H
|
||||
@@ -1,3 +1,4 @@
|
||||
cflags += --target=x86_64-pc-none-elf
|
||||
|
||||
ldflags += --target=x86_64-pc-none-elf
|
||||
ldflags += --target=x86_64-pc-none-elf \
|
||||
-Wl,-zmax-page-size=0x1000
|
||||
|
||||
105
kernel/amd64/init.c
Normal file
105
kernel/amd64/init.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <amd64/init.h>
|
||||
#include <amd64/tss.h>
|
||||
#include <amd64/debug.h>
|
||||
|
||||
#define GDT_KCODE 0x08
|
||||
#define GDT_KDATA 0x10
|
||||
#define GDT_UCODE 0x18
|
||||
#define GDT_UDATA 0x20
|
||||
#define GDT_TSS 0x28
|
||||
|
||||
#define TSS 0x80
|
||||
#define TSS_PRESENT 0x89
|
||||
|
||||
#define KSTACK_SIZE (8*1024)
|
||||
|
||||
struct gdt_entry {
|
||||
uint16_t limitlow;
|
||||
uint16_t baselow;
|
||||
uint8_t basemid;
|
||||
uint8_t access;
|
||||
uint8_t gran;
|
||||
uint8_t basehigh;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct gdt_ptr {
|
||||
uint16_t limit;
|
||||
uint64_t base;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct gdt_extended {
|
||||
struct gdt_entry old[5];
|
||||
struct gdt_entry tsslow;
|
||||
struct gdt_entry tsshigh;
|
||||
} __attribute__((packed));
|
||||
|
||||
__attribute__((aligned(16))) static volatile uint8_t kernel_stack[KSTACK_SIZE];
|
||||
__attribute__((aligned(16))) static volatile struct gdt_extended gdt;
|
||||
|
||||
static void amd64_gdt_set(volatile struct gdt_entry *ent, uint32_t base,
|
||||
uint32_t limit, uint8_t acc, uint8_t gran) {
|
||||
ent->baselow = (base & 0xFFFF);
|
||||
ent->basemid = (base >> 16) & 0xFF;
|
||||
ent->basehigh = (base >> 24) & 0xFF;
|
||||
ent->limitlow = (limit & 0xFFFF);
|
||||
ent->gran = ((limit >> 16) & 0x0F) | (gran & 0xF0);
|
||||
ent->access = acc;
|
||||
}
|
||||
|
||||
static void amd64_gdt_init(void) {
|
||||
volatile struct tss *tss = amd64_get_tss();
|
||||
|
||||
memset((void *)&gdt, 0, sizeof(gdt));
|
||||
memset((void *)kernel_stack, 0, sizeof(kernel_stack));
|
||||
memset((void *)tss, 0, sizeof(*tss));
|
||||
|
||||
tss->iopb_off = sizeof(*tss);
|
||||
tss->rsp0 = (uint64_t)((uintptr_t)kernel_stack + sizeof(kernel_stack));
|
||||
|
||||
uint64_t tssbase = (uint64_t)&tss;
|
||||
uint64_t tsslimit = sizeof(*tss) - 1;
|
||||
|
||||
amd64_gdt_set(&gdt.old[0], 0, 0, 0, 0);
|
||||
amd64_gdt_set(&gdt.old[1], 0, 0xFFFFF, 0x9A, 0xA0);
|
||||
amd64_gdt_set(&gdt.old[2], 0, 0xFFFFF, 0x92, 0xC0);
|
||||
amd64_gdt_set(&gdt.old[3], 0, 0xFFFFF, 0xFA, 0xA0);
|
||||
amd64_gdt_set(&gdt.old[4], 0, 0xFFFFF, 0xF2, 0xC0);
|
||||
amd64_gdt_set(&gdt.tsslow, (tssbase & 0xFFFFFFFF), tsslimit, TSS_PRESENT | TSS, 0);
|
||||
|
||||
uint32_t tssbasehigh = (tssbase >> 32);
|
||||
gdt.tsshigh.limitlow = (tssbasehigh & 0xFFFF);
|
||||
gdt.tsshigh.baselow = (tssbasehigh >> 16) & 0xFFFF;
|
||||
gdt.tsshigh.basemid = 0;
|
||||
gdt.tsshigh.basehigh = 0;
|
||||
gdt.tsshigh.access = 0;
|
||||
gdt.tsshigh.gran = 0;
|
||||
|
||||
struct gdt_ptr gdtr;
|
||||
gdtr.limit = sizeof(gdt) - 1;
|
||||
gdtr.base = (uint64_t)&gdt;
|
||||
__asm__ volatile("lgdt %0" :: "m"(gdtr) : "memory");
|
||||
|
||||
__asm__ volatile(
|
||||
"pushq %[kcode]\n"
|
||||
"lea 1f(%%rip), %%rax\n"
|
||||
"pushq %%rax\n"
|
||||
"lretq\n"
|
||||
"1:\n"
|
||||
"movw %[kdata], %%ax\n"
|
||||
"movw %%ax, %%ds\n"
|
||||
"movw %%ax, %%es\n"
|
||||
"movw %%ax, %%ss\n"
|
||||
:
|
||||
: [kcode] "i"(GDT_KCODE), [kdata] "i"(GDT_KDATA)
|
||||
: "rax", "memory"
|
||||
);
|
||||
|
||||
__asm__ volatile("ltr %0" :: "r"((uint16_t)GDT_TSS));
|
||||
}
|
||||
|
||||
void amd64_init(void) {
|
||||
amd64_gdt_init();
|
||||
amd64_debug_init();
|
||||
}
|
||||
6
kernel/amd64/init.h
Normal file
6
kernel/amd64/init.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef _KERNEL_AMD64_INIT_H
|
||||
#define _KERNEL_AMD64_INIT_H
|
||||
|
||||
void amd64_init(void);
|
||||
|
||||
#endif // _KERNEL_AMD64_INIT_H
|
||||
54
kernel/amd64/io.c
Normal file
54
kernel/amd64/io.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <libk/std.h>
|
||||
#include <amd64/io.h>
|
||||
|
||||
void amd64_io_outb(uint16_t port, uint8_t v) {
|
||||
__asm__ volatile("outb %1, %0" :: "dN"(port), "a"(v));
|
||||
}
|
||||
|
||||
void amd64_io_outw(uint16_t port, uint16_t v) {
|
||||
__asm__ volatile("outw %%ax, %%dx" :: "a"(v), "d"(port));
|
||||
}
|
||||
|
||||
void amd64_io_outl(uint16_t port, uint32_t v) {
|
||||
__asm__ volatile("outl %%eax, %%dx" :: "d"(port), "a"(v));
|
||||
}
|
||||
|
||||
void amd64_io_outsw(uint16_t port, const void *addr, int cnt) {
|
||||
__asm__ volatile(
|
||||
"cld; rep outsw"
|
||||
: "+S"(addr), "+c"(cnt)
|
||||
: "d"(port)
|
||||
: "memory", "cc"
|
||||
);
|
||||
}
|
||||
|
||||
uint8_t amd64_io_inb(uint16_t port) {
|
||||
uint8_t r;
|
||||
__asm__ volatile("inb %1, %0" : "=a"(r) : "dN"(port));
|
||||
return r;
|
||||
}
|
||||
|
||||
uint16_t amd64_io_inw(uint16_t port) {
|
||||
uint16_t r;
|
||||
__asm__ volatile("inw %%dx, %%ax" : "=a"(r) : "d"(port));
|
||||
return r;
|
||||
}
|
||||
|
||||
uint32_t amd64_io_inl(uint16_t port) {
|
||||
uint32_t r;
|
||||
__asm__ volatile("inl %%dx, %%eax" : "=a"(r) : "d"(port));
|
||||
return r;
|
||||
}
|
||||
|
||||
void amd64_io_insw(uint16_t port, void *addr, int cnt) {
|
||||
__asm__ volatile(
|
||||
"cld; rep insw"
|
||||
: "+D"(addr), "+c"(cnt)
|
||||
: "d"(port)
|
||||
: "memory", "cc"
|
||||
);
|
||||
}
|
||||
|
||||
void amd64_io_wait(void) {
|
||||
amd64_io_outb(0x80, 0);
|
||||
}
|
||||
16
kernel/amd64/io.h
Normal file
16
kernel/amd64/io.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _KERNEL_AMD64_IO_H
|
||||
#define _KERNEL_AMD64_IO_H
|
||||
|
||||
#include <libk/std.h>
|
||||
|
||||
void amd64_io_outb(uint16_t port, uint8_t v);
|
||||
void amd64_io_outw(uint16_t port, uint16_t v);
|
||||
void amd64_io_outl(uint16_t port, uint32_t v);
|
||||
void amd64_io_outsw(uint16_t port, const void *addr, int cnt);
|
||||
uint8_t amd64_io_inb(uint16_t port);
|
||||
uint16_t amd64_io_inw(uint16_t port);
|
||||
uint32_t amd64_io_inl(uint16_t port);
|
||||
void amd64_io_insw(uint16_t port, void *addr, int cnt);
|
||||
void amd64_io_wait(void);
|
||||
|
||||
#endif // _KERNEL_AMD64_IO_H
|
||||
@@ -1,3 +1,11 @@
|
||||
c += amd64/bootmain.c
|
||||
c += amd64/bootmain.c \
|
||||
amd64/init.c \
|
||||
amd64/tss.c \
|
||||
amd64/io.c \
|
||||
amd64/debug.c
|
||||
|
||||
o += amd64/bootmain.o
|
||||
o += amd64/bootmain.o \
|
||||
amd64/init.o \
|
||||
amd64/tss.o \
|
||||
amd64/io.o \
|
||||
amd64/debug.o
|
||||
|
||||
8
kernel/amd64/tss.c
Normal file
8
kernel/amd64/tss.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <libk/std.h>
|
||||
#include <amd64/tss.h>
|
||||
|
||||
__attribute__((aligned(16))) static volatile struct tss tss;
|
||||
|
||||
volatile struct tss *amd64_get_tss(void) {
|
||||
return &tss;
|
||||
}
|
||||
20
kernel/amd64/tss.h
Normal file
20
kernel/amd64/tss.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef _KERNEL_AMD64_TSS_H
|
||||
#define _KERNEL_AMD64_TSS_H
|
||||
|
||||
#include <libk/std.h>
|
||||
|
||||
struct tss {
|
||||
uint32_t resv0;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t resv1;
|
||||
uint64_t ist[7];
|
||||
uint64_t resv2;
|
||||
uint16_t resv3;
|
||||
uint16_t iopb_off;
|
||||
} __attribute__((packed));
|
||||
|
||||
volatile struct tss *amd64_get_tss(void);
|
||||
|
||||
#endif // _KERNEL_AMD64_TSS_H
|
||||
@@ -5,10 +5,13 @@ cflags += -nostdinc \
|
||||
-std=c11 \
|
||||
-pedantic \
|
||||
-Wall \
|
||||
-Wextra
|
||||
-Wextra \
|
||||
-mcmodel=kernel
|
||||
|
||||
cflags += -isystem . -isystem c_headers/include
|
||||
|
||||
cflags += -DPRINTF_INCLUDE_CONFIG_H=1
|
||||
|
||||
ifeq ($(buildtype),debug)
|
||||
cflags += -O0 -g
|
||||
endif
|
||||
|
||||
1
kernel/libk/.gitignore
vendored
Normal file
1
kernel/libk/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.o
|
||||
1651
kernel/libk/printf.c
Normal file
1651
kernel/libk/printf.c
Normal file
File diff suppressed because it is too large
Load Diff
242
kernel/libk/printf.h
Normal file
242
kernel/libk/printf.h
Normal file
@@ -0,0 +1,242 @@
|
||||
/**
|
||||
* @author (c) Eyal Rozenberg <eyalroz1@gmx.com>
|
||||
* 2021-2024, Haifa, Palestine/Israel
|
||||
* @author (c) Marco Paland (info@paland.com)
|
||||
* 2014-2019, PALANDesign Hannover, Germany
|
||||
*
|
||||
* @note Others have made smaller contributions to this file: see the
|
||||
* contributors page at https://github.com/eyalroz/printf/graphs/contributors
|
||||
* or ask one of the authors.
|
||||
*
|
||||
* @brief Small stand-alone implementation of the printf family of functions
|
||||
* (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems
|
||||
* with a very limited resources.
|
||||
*
|
||||
* @note the implementations are thread-safe; re-entrant; use no functions from
|
||||
* the standard library; and do not dynamically allocate any memory.
|
||||
*
|
||||
* @license The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef PRINTF_H_
|
||||
#define PRINTF_H_
|
||||
|
||||
#ifdef PRINTF_INCLUDE_CONFIG_H
|
||||
#include "printf_config.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
# include <cstdarg>
|
||||
# include <cstddef>
|
||||
extern "C" {
|
||||
#else
|
||||
# include <stdarg.h>
|
||||
# include <stddef.h>
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
# if ((__GNUC__ == 4 && __GNUC_MINOR__>= 4) || __GNUC__ > 4)
|
||||
# define ATTR_PRINTF(one_based_format_index, first_arg) \
|
||||
__attribute__((format(gnu_printf, (one_based_format_index), (first_arg))))
|
||||
# else
|
||||
# define ATTR_PRINTF(one_based_format_index, first_arg) \
|
||||
__attribute__((format(printf, (one_based_format_index), (first_arg))))
|
||||
# endif
|
||||
# define ATTR_VPRINTF(one_based_format_index) \
|
||||
ATTR_PRINTF((one_based_format_index), 0)
|
||||
#else
|
||||
# define ATTR_PRINTF(one_based_format_index, first_arg)
|
||||
# define ATTR_VPRINTF(one_based_format_index)
|
||||
#endif
|
||||
|
||||
#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
|
||||
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT 0
|
||||
#endif
|
||||
|
||||
#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
|
||||
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 0
|
||||
#endif
|
||||
|
||||
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
|
||||
# define printf_ printf
|
||||
# define sprintf_ sprintf
|
||||
# define vsprintf_ vsprintf
|
||||
# define snprintf_ snprintf
|
||||
# define vsnprintf_ vsnprintf
|
||||
# define vprintf_ vprintf
|
||||
#endif
|
||||
|
||||
/*
|
||||
* If you want to include this implementation file directly rather than
|
||||
* link against it, this will let you control the functions' visibility,
|
||||
* e.g. make them static so as not to clash with other objects also
|
||||
* using them.
|
||||
*/
|
||||
#ifndef PRINTF_VISIBILITY
|
||||
#define PRINTF_VISIBILITY
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Prints/send a single character to some opaque output entity
|
||||
*
|
||||
* @note This function is not implemented by the library, only declared; you
|
||||
* must provide an implementation if you wish to use the @ref printf / @ref
|
||||
* vprintf function (and possibly for linking against the library, if your
|
||||
* toolchain does not support discarding unused functions)
|
||||
*
|
||||
* @note The output could be as simple as a wrapper for the `write()` system
|
||||
* call on a Unix-like * system, or even libc's @ref putchar , for replicating
|
||||
* actual functionality of libc's @ref printf * function; but on an embedded
|
||||
* system it may involve interaction with a special output device, like a UART,
|
||||
* etc.
|
||||
*
|
||||
* @note in libc's @ref putchar, the parameter type is an int; this was intended
|
||||
* to support the representation of either a proper character or EOF in a
|
||||
* variable - but this is really not meaningful to pass into @ref putchar and is
|
||||
* discouraged today. See further discussion in:
|
||||
* @link https://stackoverflow.com/q/17452847/1593077
|
||||
*
|
||||
* @param c the single character to print
|
||||
*/
|
||||
PRINTF_VISIBILITY
|
||||
void putchar_(char c);
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of the C standard's printf/vprintf
|
||||
*
|
||||
* @note you must implement a @ref putchar_ function for using this function -
|
||||
* it invokes @ref putchar_ * rather than directly performing any I/O (which
|
||||
* insulates it from any dependence on the operating system * and external
|
||||
* libraries).
|
||||
*
|
||||
* @param format A string specifying the format of the output, with %-marked
|
||||
* specifiers of how to interpret additional arguments.
|
||||
* @param arg Additional arguments to the function, one for each %-specifier in
|
||||
* @p format
|
||||
* @return The number of characters written into @p s, not counting the
|
||||
* terminating null character
|
||||
*/
|
||||
/* @{ */
|
||||
PRINTF_VISIBILITY
|
||||
int printf_(const char* format, ...) ATTR_PRINTF(1, 2);
|
||||
PRINTF_VISIBILITY
|
||||
int vprintf_(const char* format, va_list arg) ATTR_VPRINTF(1);
|
||||
/* @} */
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of the C standard's sprintf/vsprintf
|
||||
*
|
||||
* @note For security considerations (the potential for exceeding the buffer
|
||||
* bounds), please consider using the size-constrained variant, @ref snprintf /
|
||||
* @ref vsnprintf, instead.
|
||||
*
|
||||
* @param s An array in which to store the formatted string. It must be large
|
||||
* enough to fit the formatted output!
|
||||
* @param format A string specifying the format of the output, with %-marked
|
||||
* specifiers of how to interpret additional arguments
|
||||
* @param arg Additional arguments to the function, one for each specifier in
|
||||
* @p format
|
||||
* @return The number of characters written into @p s, not counting the
|
||||
* terminating null character
|
||||
*/
|
||||
/* @{ */
|
||||
PRINTF_VISIBILITY
|
||||
int sprintf_(char* s, const char* format, ...) ATTR_PRINTF(2, 3);
|
||||
PRINTF_VISIBILITY
|
||||
int vsprintf_(char* s, const char* format, va_list arg) ATTR_VPRINTF(2);
|
||||
/* @} */
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of the C standard's snprintf/vsnprintf
|
||||
*
|
||||
* @param s An array in which to store the formatted string. It must be large
|
||||
* enough to fit either the entire formatted output, or at least @p n
|
||||
* characters. Alternatively, it can be NULL, in which case nothing will
|
||||
* be printed, and only the number of characters which _could_ have been
|
||||
* printed is tallied and returned.
|
||||
* @param n The maximum number of characters to write to the array, including
|
||||
* a terminating null character
|
||||
* @param format A string specifying the format of the output, with %-marked
|
||||
* specifiers of how to interpret additional arguments.
|
||||
* @param arg Additional arguments to the function, one for each specifier in
|
||||
* @p format
|
||||
* @return The number of characters that COULD have been written into @p s, not
|
||||
* counting the terminating null character. A value equal or larger than
|
||||
* @p n indicates truncation. Only when the returned value is non-negative
|
||||
* and less than @p n, the null-terminated string has been fully and
|
||||
* successfully printed.
|
||||
*/
|
||||
/* @{ */
|
||||
PRINTF_VISIBILITY
|
||||
int snprintf_(char* s, size_t count, const char* format, ...) ATTR_PRINTF(3, 4);
|
||||
PRINTF_VISIBILITY
|
||||
int vsnprintf_(char* s, size_t count, const char* format, va_list arg) ATTR_VPRINTF(3);
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* printf/vprintf with user-specified output function
|
||||
*
|
||||
* An alternative to @ref printf_, in which the output function is specified
|
||||
* dynamically (rather than @ref putchar_ being used)
|
||||
*
|
||||
* @param out An output function which takes one character and a type-erased
|
||||
* additional parameters
|
||||
* @param extra_arg The type-erased argument to pass to the output function @p
|
||||
* out with each call
|
||||
* @param format A string specifying the format of the output, with %-marked
|
||||
* specifiers of how to interpret additional arguments.
|
||||
* @param arg Additional arguments to the function, one for each specifier in
|
||||
* @p format
|
||||
* @return The number of characters for which the output f unction was invoked,
|
||||
* not counting the terminating null character
|
||||
*
|
||||
*/
|
||||
PRINTF_VISIBILITY
|
||||
int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...) ATTR_PRINTF(3, 4);
|
||||
PRINTF_VISIBILITY
|
||||
int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg) ATTR_VPRINTF(3);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
|
||||
# undef printf_
|
||||
# undef sprintf_
|
||||
# undef vsprintf_
|
||||
# undef snprintf_
|
||||
# undef vsnprintf_
|
||||
# undef vprintf_
|
||||
#else
|
||||
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
|
||||
# define printf printf_
|
||||
# define sprintf sprintf_
|
||||
# define vsprintf vsprintf_
|
||||
# define snprintf snprintf_
|
||||
# define vsnprintf vsnprintf_
|
||||
# define vprintf vprintf_
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /* PRINTF_H_ */
|
||||
6
kernel/libk/printf_config.h
Normal file
6
kernel/libk/printf_config.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef _KERNEL_LIBK_PRINTF_CONFIG_H
|
||||
#define _KERNEL_LIBK_PRINTF_CONFIG_H
|
||||
|
||||
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 1
|
||||
|
||||
#endif // _KERNEL_LIBK_PRINTF_CONFIG_H
|
||||
3
kernel/libk/putchar_.c
Normal file
3
kernel/libk/putchar_.c
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
void putchar_(char x) { (void)x; }
|
||||
|
||||
7
kernel/libk/src.mk
Normal file
7
kernel/libk/src.mk
Normal file
@@ -0,0 +1,7 @@
|
||||
c += libk/string.c \
|
||||
libk/printf.c \
|
||||
libk/putchar_.c
|
||||
|
||||
o += libk/string.o \
|
||||
libk/printf.o \
|
||||
libk/putchar_.o
|
||||
12
kernel/libk/std.h
Normal file
12
kernel/libk/std.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _KERNEL_LIBK_STD_H
|
||||
#define _KERNEL_LIBK_STD_H
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdalign.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdnoreturn.h>
|
||||
|
||||
#endif // _KERNEL_LIBK_STD_H
|
||||
44
kernel/libk/string.c
Normal file
44
kernel/libk/string.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
|
||||
size_t memset(void *dst, uint8_t b, size_t n) {
|
||||
uint8_t *dst1 = dst;
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++)
|
||||
dst1[i] = b;
|
||||
return i;
|
||||
}
|
||||
|
||||
size_t memcpy(void *dst, const void *src, size_t n) {
|
||||
uint8_t *dst1 = dst;
|
||||
const uint8_t *src1 = src;
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++)
|
||||
dst1[i] = src1[i];
|
||||
return i;
|
||||
}
|
||||
|
||||
// SOURCE: https://stackoverflow.com/a/48967408
|
||||
void strncpy(char* dst, const char* src, size_t n) {
|
||||
size_t i = 0;
|
||||
while(i++ != n && (*dst++ = *src++));
|
||||
}
|
||||
|
||||
size_t strlen(const char *str) {
|
||||
const char *s;
|
||||
for (s = str; *s; ++s);
|
||||
return (s - str);
|
||||
}
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n) {
|
||||
unsigned char *p = (unsigned char *)s1;
|
||||
unsigned char *q = (unsigned char *)s2;
|
||||
|
||||
while (n--) {
|
||||
if (*p != *q) {
|
||||
return (int)*p - (int)*q;
|
||||
}
|
||||
p++, q++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
10
kernel/libk/string.h
Normal file
10
kernel/libk/string.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef _KERNEL_LIBK_STRING_H
|
||||
#define _KERNEL_LIBK_STRING_H
|
||||
|
||||
size_t memset(void *dst, uint8_t b, size_t n);
|
||||
size_t memcpy(void *dst, const void *src, size_t n);
|
||||
void strncpy(char* dst, const char* src, size_t n);
|
||||
size_t strlen(const char *str);
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
#endif // _KERNEL_LIBK_STRING_H
|
||||
10
kernel/sys/debug.h
Normal file
10
kernel/sys/debug.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef _KERNEL_SYS_DEBUG_H
|
||||
#define _KERNEL_SYS_DEBUG_H
|
||||
|
||||
void debugprintf(const char *fmt, ...);
|
||||
|
||||
#define DEBUG(fmt, ...) do { \
|
||||
debugprintf("%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
#endif // _KERNEL_SYS_DEBUG_H
|
||||
Reference in New Issue
Block a user