osdev liveblog #563

 
citrons (bureaucrat) (edited ) src #5152/1

I'm porting this from apionet.


r​aven[x]: it begins.
r​aven[x]: https://git.citrons.xyz/tinycc
o​live[x]: are you aware you can push directly to tinycc proper
r​aven[x]: what do you mean?
o​live[x]: they have a "mob" branch
o​live[x]: the idea is like wikipages
o​live[x]: anyone can push or so
o​live[x]: repo.or.cz has an explanation page somewhere
r​aven[x]: so, I could commit malware to the mob branch right now?
o​live[x]: https://repo.or.cz/h/mob.html
o​live[x]: plausibly
o​live[x]: just as one can add disinformation to wikipedia
o​live[x]: but that is easier to spot
r​aven[x]: that's true, but ugh.
r​aven[x]: the last actual commit to master was 8 years ago
o​live[x]: the tinycc maintainer is awol
o​live[x]: yea
r​aven[x]: I like the idea of "finished software" that stops receiving 
updates. everyone is always saying "dead project!!!1! it's not maintained!!!!"
r​aven[x]: however.
r​aven[x]: tcc is far from finished. that's why I'm working on it
o​live[x]: lots of Luanti mods are effectively "finished"
v​iba[x]: > I like the idea of "finished software" that stops receiving 
updates. everyone is always saying "dead project!!!1! it's not maintained!!!!"
v​iba[x]: im still trying to turn off the part of my brain that instantly 
discards things based on the last commit date
r​aven[x]: hm, ok, I'm kind of starting to get how this works in practice
r​aven[x]: 
https://repo.or.cz/tinycc.git/commit/d9a6d9aec058ec92c1ad19ccf9e7913bc8ea945f
o​live[x]: miu i can't see what's behind this link
r​aven[x]: 
https://web.archive.org/https://repo.or.cz/tinycc.git/commit/d9a6d9aec058ec92c1ad19ccf9e7913bc8ea945f


r​aven[x]: ok, I just pushed to the mob branch. hi mom
r​aven[x]: I did --unset-upstream so I don't accidentally do that.
r​aven[x]: because I can run git push sort of reflexively sometimes
r​aven[x]: this is kind of neat. I mean, it's a little scarier of an idea 
than wikipedia in my opinion.
r​aven[x]: because you can't put malware on wikipedia. at least, not easily.


r​aven[x]: I think it would be interesting to try to take the pseudocode in 
architecture manuals and create an interpreter for it
r​aven[x]: thus deriving an emulator directly from the specification


r​aven[x]: the assembler does not support PC-relative ADR or LDR. evidently 
these aren't needed in the C compiler. the relocations they require are not 
implemented in the linker, so to implement them, I'll have to touch the linker
r​aven[x]: however, actually, that makes me wonder what it does instead.
r​aven[x]: the limited featureset of the linker implies that you'd probably 
have trouble linking code generated by other compilers
r​aven[x]: ah. I see.
r​aven[x]: you can just do ldr r0, [pc], offset
r​aven[x]: um, yes, so, it puts the constant in the middle of the code, loads 
with pc as the base register, and then jumps over the constant
r​aven[x]: that seems at least somewhat suboptimal
r​aven[x]: nonetheless, I suppose I could use the same approach in my 
assembler code and avoid hacking on TCC anymore
r​aven[x]: because although I think it would be very worthwhile to improve 
TCC, it's distracting from the project I'm using TCC for.
r​ebecca[x]: what's the project?
r​aven[x]: I've contracted osdev disease again.
r​aven[x]: I want to make my operating system with TCC, so then I can port 
TCC to my operating system, and then have it be selfhosting.

my first target is raspberry pi 3/4

citrons (bureaucrat) src #5153/0

the raspberry pi's firmware loads a flat binary into memory. it doesn't understand ELF images or anything; it just starts executing from the beginning. the osdev wiki tells you to use GNU linker scripts, but as I am not using binutils, I'm going to use the advanced linker called "cat."

I'm going to have a tiny loader that is compiled as a flat binary. an ELF image of the kernel will be concatenated to the end of it. the loader will peek into the ELF header, hollow out .bss, and jump into the entry point.

if I understand how ELF images work correctly, this should work.

citrons (bureaucrat) src #5154/0

ok, well, I've been staring at objdump output, not necessarily understanding what it all means, and I think it might expect me to map things where they don't end up necessarily just by blindly copying it into memory.

citrons (bureaucrat) src #5155/0

if I can't make this work easily, it might actually just be better to have my entire kernel be a single compilation unit, compile it all as a flat binary, and presumably everything will appear in the order I specify it.

I wonder, actually, if I could have separate compilation units, but name the unit containing the init code alphabetically before the others. that would be such a nasty hack, haha.

ubq323 (bureaucrat) src #5156/0

if you're eschewing binutils and friends, what's the advantage of using elf images as opposed to flat binaries or some custom format?

citrons (bureaucrat) src #5157/0

because TCC emits ELF

citrons (bureaucrat) (edited ) src #5158/1

it can also emit flat binaries, but it's unclear where the entry point is. of course, that can be solved by my "having everything be in one compilation unit" idea, which I'm starting to think would be a good idea, especially considering TCC's breakneck compilation speeds.

citrons (bureaucrat) src #5159/0

a reasonable question would be: "how are you going to write an entry point in assembler if you're compiling everything as one C compilation unit?" of course, the answer to that is that mercifully and quite conveniently, TCC supports asm at the toplevel.

citrons (bureaucrat) (edited ) src #5160/1

you know, I'm actually pretty sure that if I just specify the entry point object file first in the command line arguments, it will probably put that first in the file in the beginning of the output. I'll just try doing that.

citrons (bureaucrat) src #5161/0

nope, I'm totally wrong. it did not do that.

citrons (bureaucrat) src #5162/0

also, the file has a ton of extra empty space for some reason

citrons (bureaucrat) src #5163/0

I specified my init code as the first argument to the linker, but the first thing that appears in the file is a string literal.

citrons (bureaucrat) src #5164/0

hm, it seems that the code starts at 0x90000. indeed, my entry point is ordered before everything else, but only after kilobytes of data and zeros

citrons (bureaucrat) (edited ) src #5165/1

oh! putting my init code is a separate section (I called it .text.init) does put it at the start of the file.

ubq323 (bureaucrat) src #5166/0

but only after kilobytes of data and zeros

what could possibly cause this?

citrons (bureaucrat) src #5167/0

alignment

citrons (bureaucrat) src #5168/0

booting my image in qemu, it of course does not work the first time. I'm having to debug the classic issue of "it's jumping somewhere random in memory for some reason." of course, the issue seems unrelated to linker nonsense, so that's good. it's probably a silly mistake of mine.

citrons (bureaucrat) src #5169/0

ok, looking at this code TCC generated, I have no clue why there is a massive amount of nops everywhere whatsoever.

citrons (bureaucrat) src #5170/0

oh, maybe it's for alignment or something.

citrons (bureaucrat) src #5171/0

wait a minute! tcc generated adrp! that's not supported in the assembler! I was under the impression that the C compiler couldn't generate it either because the linker doesn't seem to handle the relocation that's required.

citrons (bureaucrat) src #5174/0

it works!!!

void
kmain() {
    initpi();
    uartputs("hey bitches!\n");
    while (1) uartputc(uartgetc());
}
% qemu-system-aarch64 -M raspi3b -serial stdio -kernel pi3.img 
hey bitches!
citrons (bureaucrat) src #5187/0

so, I'm going to implement SMP as soon as possible, so that I produce code that is amenable to parallelism from the very start. as it turns out, ARM has some very neat instructions to put cores in a low power state for waiting for things.

ARMv7 provides a locking mechanism based on events. The Wait For Event lock mechanism permits a processor that has failed to obtain a lock to enter a low-power state. When the processor that currently holds the required lock releases the lock it sends an event that causes any waiting processors to wake up and attempt to gain the lock again.

this is what wfe is for. you go to sleep with your w(i)fe. then, you're awoken by sev. sev wakes up all the processors that are waiting, and they check their respective locks/semaphores/what have you.

citrons (bureaucrat) src #5188/0

I've done some more work on TCC to add the instructions to the assembler required for synchronization primitives.

citrons (bureaucrat) (edited ) src #5189/1

GAH. tabs slipped in. I hate fucking with editor config. why doesn't everyone just use fucking tabs.

citrons (bureaucrat) (edited ) src #5190/1

I now have an untested spinlock compiling on TCC.

static U8
casb(volatile U8 *p, U8 old, U8 new) {
    U8 cur;
    while (1) {
        asm volatile("ldaxrb %0, %1" : "=r" (cur) : "m" (*p)); 
        if (cur != old) {
            asm volatile("dmb ish" : : : "memory");
            break;
        }
        int r;
        asm volatile("stlxrb %0, %1, %2" : "=&r" (r) : "r" (new), "m" (*p));
        if (r) break;
    }
    return cur;
}

void
spinlock(Spinlock *s) {
    while (1) {
        if (casb(s, 0, 1) == 0) break;
        asm volatile("wfe");
    }
}

void
spinunlock(Spinlock *s) {
    casb(s, 1, 0);
    asm volatile("sev");
}

it's pretty frustrating when I want to do something in TCC and then it says, "sorry, can't do that thing." but then, I can hack on it and then it does that thing. the only drawback is that now it's 7am.

citrons (bureaucrat) src #5191/0

I had to squint my eyes at the ARM manual for hours to start understanding how everything fits together, so it took me a while to implement all these instructions. however, as I progressively page more and more of this information into my brain, it will probably become easier and I will be able to work a lot faster.

citrons (bureaucrat) src #5203/0

if I generate a flat binary, I lack debugging symbols. this is making debugging difficult. even if I separately compile an ELF, the symbols don't end up in the same place.

most kernels need to be mapped into the higher half of memory. so, remapping pages is something I already need to do. so, what I'm going to do is go back to my idea of appending an ELF image onto the flat binary, but I'm going to read the ELF properly and map its segments, and I'm going to compile it targeting the higher half of memory.

please log in to reply to this thread