Skip to content

Commit 4a0fcfd

Browse files
committed
os: Implement new default stdout for x86-pc
1 parent 22c68f8 commit 4a0fcfd

6 files changed

Lines changed: 21 additions & 14 deletions

File tree

api/kernel/os.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,18 @@ class OS {
131131
/**
132132
* Write data to standard out callbacks
133133
*/
134-
static size_t print(const char* ptr, const size_t len);
134+
static void print(const char* ptr, const size_t len);
135135

136136
/**
137137
* Add handler for standard output.
138138
*/
139139
static void add_stdout(print_func func);
140140
/**
141-
* Add stdout handler that calls OS::default_stdout
141+
* Add stdout handler that simply calls OS::default_stdout
142142
**/
143-
static void add_default_stdout();
143+
static void add_default_stdout() {
144+
add_stdout(OS::default_stdout);
145+
}
144146

145147
/**
146148
* The default output method preferred by each platform

src/include/kprint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333
#endif
3434

3535
extern void __serial_print1(const char* cstr);
36+
extern void __serial_print(const char* str, size_t len);
3637

3738
/**
3839
* The earliest possible print function (requires no heap, global ctors etc.)

src/kernel/os.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,15 @@ void OS::add_stdout(OS::print_func func)
101101
{
102102
os_print_handlers.add(func);
103103
}
104-
void OS::add_default_stdout()
105-
{
106-
add_stdout(
107-
[] (const char* str, const size_t len) {
108-
OS::default_stdout(str, len);
109-
});
110-
}
111104
__attribute__ ((weak))
112105
void default_stdout_handlers()
113106
{
114107
OS::add_default_stdout();
115108
}
116-
size_t OS::print(const char* str, const size_t len)
109+
void OS::print(const char* str, const size_t len)
117110
{
118111
for (auto& func : os_print_handlers)
119112
func(str, len);
120-
return len;
121113
}
122114

123115
void OS::legacy_boot() {

src/platform/x86_pc/os.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include <cstdio>
2222
#include <boot/multiboot.h>
23-
#include <hw/cmos.hpp>
2423
#include <kernel/os.hpp>
2524
#include <kernel/irq_manager.hpp>
2625
#include <kernel/rtc.hpp>
@@ -96,6 +95,10 @@ void OS::halt() {
9695
*os_cycles_hlt += cycles_since_boot() - *os_cycles_total;
9796
}
9897

98+
void OS::default_stdout(const char* str, const size_t len)
99+
{
100+
__serial_print(str, len);
101+
}
99102

100103
void OS::start(uint32_t boot_magic, uint32_t boot_addr)
101104
{

src/platform/x86_pc/serial1.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ void __serial_print1(const char* cstr)
2222
hw::outb(port, *cstr++);
2323
}
2424
}
25+
extern "C"
26+
void __serial_print(const char* str, size_t len)
27+
{
28+
for (size_t i = 0; i < len; i++) {
29+
while (not (hw::inb(port + 5) & 0x20));
30+
hw::outb(port, str[i]);
31+
}
32+
}

src/posix/unistd.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ int read(int fd, void* buf, size_t len)
9393
int write(int fd, const void* ptr, size_t len)
9494
{
9595
if (fd < 4) {
96-
return OS::print((const char*) ptr, len);
96+
OS::print((const char*) ptr, len);
97+
return len;
9798
}
9899
else if (fd == rng_fd) {
99100
rng_absorb(ptr, len);

0 commit comments

Comments
 (0)