Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve "Add Kernel Logger" #372

Merged
merged 9 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions Kernel/Bootloader/Handoff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Library/stdio.hpp>
#include <Library/string.hpp>
#include <Memory/paging.hpp>
#include <Logger.hpp>
#include <Panic.hpp>
// Generic devices
#include <Devices/Graphics/console.hpp>
Expand Down Expand Up @@ -46,7 +47,7 @@ Handoff::Handoff(void* handoff, uint32_t magic)
, m_magic(magic)
{
// Parse the handle based on the magic
debugf("Bootloader info at 0x%p\n", handoff);
Logger::Info(__func__, "Bootloader info at 0x%p", handoff);
if (magic == STIVALE2_MAGIC) {
m_bootType = Stivale2;
parseStivale2(this, handoff);
Expand All @@ -64,23 +65,24 @@ Handoff::Handoff(void* handoff, uint32_t magic)

void Handoff::parseStivale2(Handoff* that, void* handoff)
{
debugf("Booted via Stivale2\n");
Logger::Info(__func__, "Booted via Stivale2");
// Walk the list of tags in the header
struct stivale2_struct* fixed = (struct stivale2_struct*)handoff;
struct stivale2_tag* tag = (struct stivale2_tag*)(fixed->tags);
while (tag) {
switch (tag->identifier) {
case STIVALE2_STRUCT_TAG_MEMMAP_ID: {
auto memmap = (struct stivale2_struct_tag_memmap*)tag;
debugf("Found %Lu Stivale2 memmap entries.\n", memmap->entries);
if (memmap->entries > that->m_memoryMap.Count())
Logger::Debug(__func__, "Found %Lu Stivale2 memmap entries.", memmap->entries);
if (memmap->entries > that->m_memoryMap.Count()) {
panic("Not enough space to add all memory map entries!");
}
// Follows the tag list order in stivale2.h
for (size_t i = 0; i < memmap->entries; i++) {
auto entry = memmap->memmap[i];
uint64_t end = entry.base + entry.length - 1;
that->m_memoryMap[i] = Memory::Section(entry.base, end);
debugf("[%zu] 0x%08LX-0x%08LX 0x%08LX\n", i, entry.base, end, entry.length);
Logger::Debug(__func__, "[%zu] 0x%08LX-0x%08LX 0x%08LX", i, entry.base, end, entry.length);
// TODO: Make this a map that can be indexed
switch (entry.type) {
case STIVALE2_MMAP_USABLE:
Expand Down Expand Up @@ -121,25 +123,28 @@ void Handoff::parseStivale2(Handoff* that, void* handoff)
case STIVALE2_STRUCT_TAG_CMDLINE_ID: {
auto cmdline = (struct stivale2_struct_tag_cmdline*)tag;
that->m_cmdline = (char*)(cmdline->cmdline);
debugf("Stivale2 cmdline: '%s'\n", that->m_cmdline);
Logger::Debug(__func__, "Stivale2 cmdline: '%s'", that->m_cmdline);
parseCommandLine(that->m_cmdline);
break;
}
case STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID: {
auto framebuffer = (struct stivale2_struct_tag_framebuffer*)tag;
debugf("Stivale2 framebuffer:\n");
debugf("\tAddress: 0x%08LX\n", framebuffer->framebuffer_addr);
debugf("\tResolution: %ux%ux%u\n",
Logger::Debug(
__func__,
"Stivale2 framebuffer:\n"
"\tAddress: 0x%08LX\n"
"\tResolution: %ux%ux%u\n"
"\tPixel format:\n"
"\t\tRed size: %u\n"
"\t\tRed shift: %u\n"
"\t\tGreen size: %u\n"
"\t\tGreen shift: %u\n"
"\t\tBlue size: %u\n"
"\t\tBlue shift: %u",
framebuffer->framebuffer_addr,
framebuffer->framebuffer_width,
framebuffer->framebuffer_height,
framebuffer->framebuffer_bpp);
debugf("\tPixel format:\n"
"\t\tRed size: %u\n"
"\t\tRed shift: %u\n"
"\t\tGreen size: %u\n"
"\t\tGreen shift: %u\n"
"\t\tBlue size: %u\n"
"\t\tBlue shift: %u\n",
framebuffer->framebuffer_bpp,
framebuffer->red_mask_size,
framebuffer->red_mask_shift,
framebuffer->green_mask_size,
Expand All @@ -163,14 +168,15 @@ void Handoff::parseStivale2(Handoff* that, void* handoff)
break;
}
default: {
// debugf("Unknown Stivale2 tag: 0x%016X\n", tag->identifier);
Logger::Debug(__func__, "Unknown Stivale2 tag: 0x%016LX", tag->identifier);
break;
}
}

tag = (struct stivale2_tag*)tag->next;
}
debugf("Done\n");

Logger::Debug(__func__, "Done parsing Stivale2 tags");
}

} // !namespace Handoff
7 changes: 4 additions & 3 deletions Kernel/Devices/Graphics/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <Devices/Graphics/graphics.hpp>
#include <Library/stdio.hpp>
#include <Locking/Mutex.hpp>
#include <Logger.hpp>
#include <stddef.h>

namespace Console {
Expand Down Expand Up @@ -177,7 +178,7 @@ static int putchar(unsigned c, void** ptr)
}
// Clear by resetting the double buffer and swapping.
// TODO: Find a better way to do this?
debugf("Clearing screen\n");
Logger::Debug(__func__, "Clearing screen");
Graphics::resetDoubleBuffer();
Graphics::swap();
} else if (c >= '0' && c <= '9') { // just another digit of a value
Expand Down Expand Up @@ -217,14 +218,14 @@ static int putchar(unsigned c, void** ptr)
// Move to the next line
// TODO: Get width of "screen" (replace 80 with width) (#275)
if (cursorX >= 80) {
debugf("Move to the next line\n");
Logger::Debug(__func__, "Move to the next line");
cursorX = 0;
cursorY++;
}
// Clear the screen
// TODO: Get height of "screen" (replace 25 with height) (#275)
if (cursorY >= 25) {
debugf("Shift up the screen\n");
Logger::Debug(__func__, "Shift up the screen");
//TODO: Shift text up and reset the bottom line
cursorX = 0;
cursorY = 25 - 1;
Expand Down
3 changes: 2 additions & 1 deletion Kernel/Devices/Graphics/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <Bootloader/Handoff.hpp>
#include <Library/stdio.hpp>
#include <Library/string.hpp>
#include <Logger.hpp>

namespace Graphics {

Expand All @@ -37,7 +38,7 @@ void init(Framebuffer* fb)
if (!info->getAddress())
return;
// Map in the framebuffer
debugf("==== MAP FRAMEBUFFER ====\n");
Logger::Debug(__func__, "==== MAP FRAMEBUFFER ====");
Memory::mapKernelRangeVirtual(Memory::Section(
(uintptr_t)info->getAddress(),
(uintptr_t)info->getAddress() + (info->getPitch() * info->getHeight())
Expand Down
62 changes: 34 additions & 28 deletions Kernel/Devices/Serial/rs232.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
*
*/

#include <stdarg.h>
#include <Arch/Arch.hpp>
#include <Devices/Serial/rs232.hpp>
#include <Memory/heap.hpp>
#include <Library/RingBuffer.hpp>
#include <Library/stdio.hpp>
#include <Locking/RAII.hpp>
#include <Library/string.hpp>
#include <Library/RingBuffer.hpp>
#include <Locking/RAII.hpp>
#include <Logger.hpp>
#include <Memory/heap.hpp>
#include <stdarg.h>

#define RS_232_COM1_IRQ 0x04
#define RS_232_COM3_IRQ 0x04
Expand All @@ -44,24 +45,29 @@ static Mutex mutex_rs232("rs232");
static int received();
static int is_transmit_empty();
static char read_byte();
static void callback(struct registers *regs);
static void callback(struct registers* regs);

static int received() {
static int received()
{
return readByte(rs_232_port_base + RS_232_LINE_STATUS_REG) & 1;
}

static int is_transmit_empty() {
static int is_transmit_empty()
{
return readByte(rs_232_port_base + RS_232_LINE_STATUS_REG) & 0x20;
}

static char read_byte() {
static char read_byte()
{
lockedRegion([]() {
while (received() == 0);
}, mutex_rs232);
while (received() == 0)
;
},
mutex_rs232);
return readByte(rs_232_port_base + RS_232_DATA_REG);
}

static int vprintf_helper(unsigned c, void **ptr)
static int vprintf_helper(unsigned c, void** ptr)
{
// Unfortunately very hacky...
(void)ptr;
Expand All @@ -77,7 +83,7 @@ int vprintf(const char* fmt, va_list args)
return retval;
}

int printf(const char *format, ...)
int printf(const char* format, ...)
{
va_list args;
int ret_val;
Expand All @@ -88,24 +94,23 @@ int printf(const char *format, ...)
return ret_val;
}

static void callback(struct registers *regs) {
static void callback(struct registers* regs)
{
(void)regs;
// Grab the input character
char in = read_byte();
// Change carriage returns to newlines
if (in == '\r') {
in = '\n';
}
// Create a string and print it so that the
// user can see what they're typing.
char str[2] = {in, '\0'};
printf("%s", str);
printf("%c", in);
// Add the character to the circular buffer
ring.Enqueue(in);
}

// FIXME: Use separate ring buffers for COM1 & COM2
void init(uint16_t com_id) {
void init(uint16_t com_id)
{
// Register the IRQ callback
rs_232_port_base = com_id;
uint8_t IRQ = 0x20 + (com_id == RS_232_COM1 ? RS_232_COM1_IRQ : RS_232_COM2_IRQ);
Expand All @@ -122,8 +127,9 @@ void init(uint16_t com_id) {
writeByte(rs_232_port_base + RS_232_LINE_CONTROL_REG, 0x00);
// re-enable interrupts
writeByte(rs_232_port_base + RS_232_INTERRUPT_ENABLE_REG, 0x01);
// Print out header info to the serial
printf("%s",

Logger::addWriter(vprintf);
Logger::Print(
"\033[93m\n"
" _ __ _ _____\n"
" | |/ /_ _______(_)____ _ _|__ /\n"
Expand All @@ -132,24 +138,23 @@ void init(uint16_t com_id) {
"/_/|_\\__, /_/ /_/____/ |___/____/\n"
" /____/\n"
"\n\033[0m"
"Xyris Serial Output Debugger\n\n"

);
"Xyris Serial Output Debugger\n\n");
}

size_t read(char* buf, size_t count) {
size_t read(char* buf, size_t count)
{
size_t bytes = 0;
mutex_rs232.lock();
for (size_t idx = 0; idx < count && !ring.IsEmpty(); idx++)
{
for (size_t idx = 0; idx < count && !ring.IsEmpty(); idx++) {
buf[idx] = ring.Dequeue();
bytes++;
}
mutex_rs232.unlock();
return bytes;
}

size_t write(const char* buf, size_t count) {
size_t write(const char* buf, size_t count)
{
// Wait for previous transfer to complete
while (is_transmit_empty() == 0);
size_t bytes = 0;
Expand All @@ -160,7 +165,8 @@ size_t write(const char* buf, size_t count) {
return bytes;
}

int close() {
int close()
{
return 0;
}

Expand Down
28 changes: 11 additions & 17 deletions Kernel/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@
* @copyright Copyright the Xyris Contributors (c) 2019
*
*/
#include "Logger.hpp"
#include "Panic.hpp"
// System library functions
#include <Library/stdio.hpp>
#include <Library/time.hpp>
#include <stdint.h>
#include <Scheduler/tasks.hpp>
#include <Panic.hpp>
// Bootloader
#include <Bootloader/Handoff.hpp>
// Architecture specific code
#include <Arch/Arch.hpp>
// Memory management & paging
#include <Memory/paging.hpp>
// Generic devices
#include <Devices/Graphics/graphics.hpp>
#include <Devices/Graphics/console.hpp>
#include <Devices/Clock/rtc.hpp>
#include <Devices/Serial/rs232.hpp>
#include <Devices/Graphics/console.hpp>
#include <Devices/Graphics/graphics.hpp>
#include <Devices/PCSpeaker/spkr.hpp>
#include <Devices/Serial/rs232.hpp>
// Apps
#include <Applications/primes.hpp>
#include <Applications/spinner.hpp>
// Meta
#include <Support/defines.hpp>
#include <stdint.h>

static void printSplash();
static void bootTone();
Expand Down Expand Up @@ -80,31 +81,24 @@ static void bootTone()
*/
void kernelEntry(void* info, uint32_t magic)
{
// Initialize the CPU
Arch::CPU::init();
// Initialize devices
Arch::CPU::criticalRegion(devInit);
// Initialize info from bootloader

Boot::Handoff handoff(info, magic);
Memory::init(handoff.MemoryMap());
Graphics::init(handoff.FramebufferInfo());
// Print the splash screen to show we've booted into the kernel properly.
tasks_init();

printSplash();
// Print some info to show we did things right
Time::TimeDescriptor time;
Console::printf(DBG_INFO "UTC: %i/%i/%i %i:%i\n",
Console::printf("UTC: %i/%i/%i %i:%i\n",
time.getMonth(),
time.getDay(),
time.getYear(),
time.getHour(),
time.getMinutes());
// Get the CPU vendor and model data to print
const char* vendor = Arch::CPU::vendor();
const char* model = Arch::CPU::model();
Console::printf(DBG_INFO "%s %s\n", vendor, model);
RS232::printf("%s\n%s\n", vendor, model);
Logger::Info(__func__, "%s\n%s\n", Arch::CPU::vendor(), Arch::CPU::model());

tasks_init();
struct task compute, status, spinner;
tasks_new(Apps::find_primes, &compute, TASK_READY, "prime_compute");
tasks_new(Apps::show_primes, &status, TASK_READY, "prime_display");
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Library/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ int printf_helper(const char* fmt, va_list args, printf_cb_fnptr_t fn, void* ptr
break;

if (*fmt == 'z') {
#if (UINTPTR_MAX == 0xFFFFFFFF)
#if (UINTPTR_MAX == UINT32_MAX)
flags |= PR_32;
#elif (UINTPTR_MAX == 0xFFFFFFFFFFFFFFFF)
#elif (UINTPTR_MAX == UINT64_MAX)
flags |= PR_64;
#else
#error "Unknown UINTPTR_MAX value! Cannot compile printf_helper!"
Expand Down
Loading