[J-core] A bare metal hello world kernel.

Rob Landley rob at landley.net
Fri Jan 15 10:46:13 UTC 2021


Rich wrote a "hello world" kernel that the bootloader can load and relocate just
like the linux kernel (replacing the file "vmlinux" on the sd card). It's an 872
byte binary (532 bytes stripped) which prints "hello world" and then echoes back
its input (converted to uppercase). It runs on the bare metal and reads/writes
the serial port by spinning on the registers (wait for data/space, read or write
next byte).

In theory you could "sh2eb-linux-muslfdpic-objcopy -O binary vmlinux boot.bin"
something like this and use it as a boot rom in the bitstream. (Although I think
you'd skip the text-segment argument in that case, leaving it zero? I should try
that. And of course this doesn't have the DRAM init logic, so could only use
sram if you skipped the existing bootloader...)

I refactored Rich's code into one file, which compiles like this:

sh2eb-linux-muslfdpic-gcc -Os -nostartfiles -nostdlib -mno-fdpic \
  -Wl,-Ttext-segment=0x10000000 hello.c -o vmlinux

The program itself is:

void uartlite_read(volatile unsigned *uart, unsigned char *buf, int len)
{
	while (len--) {
		while (!(uart[2]&1));
		*buf++ = *uart;
	}
}

void uartlite_write(volatile unsigned *uart, char *s, int len)
{
	while (len--) {
		while ((uart[2]&8));
		uart[1] = *s++;
	}
}

void _start()
{
	volatile unsigned *ul0 = (void *)0xabcd0100;
	char *s = "hello world\r\n", c;

	uartlite_write(ul0, s, 13);
	for (;;) {
		uartlite_read(ul0, &c, 1);
		if (c>='a' && c<='z') c -= 32;
		uartlite_write(ul0, &c, 1);
	}
}


More information about the J-core mailing list