Skip to content

Commit

Permalink
tests: allow test kernels to print to serial
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Feb 22, 2018
1 parent 66f44ea commit bcea931
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tests/data/multiboot/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CCFLAGS=-m32 -Wall -Wextra -Werror -fno-stack-protector -nostdinc -fno-builtin
ASFLAGS=-m32
OUTPUT=COM1

UNAME_S!=uname -s
LIBGCC!=$(CC) ${CCFLAGS} -print-libgcc-file-name
Expand Down Expand Up @@ -27,7 +28,7 @@ modules.elf: start.o modules.o libc.o
$(LD) $(LDFLAGS) -o $@ $> $(LIBGCC)

.c.o:
$(CC) $(CCFLAGS) -c -o $@ $<
$(CC) -DOUTPUT=$(COM1) $(CCFLAGS) -c -o $@ $<

.S.o:
$(CC) $(ASFLAGS) -c -o $@ $<
22 changes: 21 additions & 1 deletion tests/data/multiboot/libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "libc.h"

static char serial_set_up = 0;

void* memcpy(void *dest, const void *src, int n)
{
char *d = dest;
Expand All @@ -46,7 +48,25 @@ void* memset(void *b, int c, unsigned len)

static void print_char(char c)
{
outb(0xe9, c);
#if (OUTPUT != DEBUGCON)
/* Check if serial is set up */
if (!serial_set_up) {
outb(OUTPUT + 1, 0x00); // Disable all interrupts
outb(OUTPUT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(OUTPUT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(OUTPUT + 1, 0x00); // (hi byte)
outb(OUTPUT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(OUTPUT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(OUTPUT + 4, 0x0B); // IRQs enabled, RTS/DSR set
serial_set_up = 1;
}
/* Check if transmit is empty */
while ( (inb(OUTPUT + 5) & 0x20) == 0 );
if ( c == '\n' )
print_char('\r');
#endif

outb(OUTPUT, c);
}

static void print_str(char *s)
Expand Down
18 changes: 18 additions & 0 deletions tests/data/multiboot/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
#ifndef LIBC_H
#define LIBC_H

/* Serial Ports */
#define DEBUGCON 0xe9
#define COM1 0x3f8
#define COM2 0x2F8
#define COM3 0x3E8
#define COM4 0x2E8

#ifndef OUTPUT
#define OUTPUT DEBUGCON
#endif

/* Integer types */

typedef unsigned long long uint64_t;
Expand Down Expand Up @@ -53,6 +64,13 @@ static inline void outb(uint16_t port, uint8_t data)
asm volatile ("outb %0, %1" : : "a" (data), "Nd" (port));
}

static inline uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb %1, %0" : "=a"(ret) : "Nd"(port) );
return ret;
}


/* Misc functions */

Expand Down

0 comments on commit bcea931

Please sign in to comment.