Live data from Hacker News

Hobby x86 kernel written with Zig

github.com

51–60 of 229 posts

Re: Hobby x86 kernel written with Zig

#51
post #38

Earlier quoted context omitted.

On the other hand safe Rust code will be memory safe..

not if you have to keep using unsafe blocks.

As an ancestor in this thread pointed out: it's hard/impossible to go without unsafe in a kernel.

Proof (and they have a policy to only use unsafe when not otherwise possible):

https://github.com/redox-os/kernel/search?q=unsafe&unscoped_...

Re: Hobby x86 kernel written with Zig

#52

Hi, author here! I've just finished writing the pre-emptive multitasking [0](only round robbin though, nothing fancy). I'm currently writing an ATA driver [1], the idea is to implement ext2. I used to do this in Rust but I switched to zig for maintainability and readability over Rust. It seems that with `comptime` I'm able to make a lot of things optimal. Overall I have to say kernel programming is _hard_ but very re…

This looks really cool, well done. Do you mind sharing the resources you’ve used so far? I see many, many tutorials on OSDev...

OSDev can be hit or miss. All of the bootstrapping was done following this [0] tutorial for Rust, which translates easily to zig. For the more advanced parts you can find usefull ones in [1] or use the OSDev search bar. For even more advanced topics though you'll find that there are no tutorial and only a few open source implementations to take inspiration from!

ps: I've updated my readme with a few references

[0] https://os.phil-opp.com/

[1] https://wiki.osdev.org/Tutorials

Re: Hobby x86 kernel written with Zig

#53
post #29

Earlier quoted context omitted.

I don't mind opinionated coding style (my Rust integration scripts all enforce that stock "rust clippy" doesn't return any error) and I do think that using tabs for indentation simply doesn't work in practice regardless of how great they are in theory because hardly anybody uses them correctly (including the vast majority of code editors by default). It might be strange to back it straight into the compiler but I don…

> including the vast majority of code editors by default Which ones exactly? Not really a problem I've encountered often except when someone tries to mix both spaces and tabs, and in general editors are built with the existence of this very common character in mind. People tend to hit the tab key to indent anyway, and one tab char meaning one level of indent is perfectly intuitive and allows users to individually con…

the problem i encounter is when you try to break a long line into multiple lines. if you want to use tabs and align the continuation, you should be mixing spaces and tabs.

for example ('-' is tab, '.' is space):

    --function_with_lots_of_arguments(arg1, arg2, arg3,
    --................................arg4, arg5, arg6);
it can be done, but a lot of editors get it wrong and it requires paying attention to the whitespace.

of course, another style would be to just indent the continuation twice, without aligning it. (i personally prefer to align continuations.)

Re: Hobby x86 kernel written with Zig

#54
post #15
post #9

Earlier quoted context omitted.

I'm curious too, I'm quite familiar with Rust and never written any Zig in my life so I went digging through the source and I find the syntax remarkably similar for the most part. The only thing that stood out is that apparently you can drop the braces for single-line `if` bodies like in C whereas Rust makes them always mandatory but I'm firmly on Rust's side on this one. The part where Rust can get really messy is w…

If you want more syntax weirdness: tab characters are illegal.

I'm assuming it's only illegal as whitespace? In other words, they're allowed in string literals, right?

Re: Hobby x86 kernel written with Zig

#55
post #6

Hi, author here! I've just finished writing the pre-emptive multitasking [0](only round robbin though, nothing fancy). I'm currently writing an ATA driver [1], the idea is to implement ext2. I used to do this in Rust but I switched to zig for maintainability and readability over Rust. It seems that with `comptime` I'm able to make a lot of things optimal. Overall I have to say kernel programming is _hard_ but very re…

> I used to do this in Rust but I switched to zig for maintainability and readability over Rust. Can you expand on this ? I'm asking out of curiosity because I want to learn a "system" programming language (for whatever definition there is to this term). So far I briefly tried Rust and Nim and found the former more difficult to read. I know nothing about Zig, how would you place it between these two ?

and somehow you forgot about D

Re: Hobby x86 kernel written with Zig

#56
post #51

Earlier quoted context omitted.

not if you have to keep using unsafe blocks.

As an ancestor in this thread pointed out: it's hard/impossible to go without unsafe in a kernel. Proof (and they have a policy to only use unsafe when not otherwise possible): https://github.com/redox-os/kernel/search?q=unsafe&unscoped_...

yes, that's the point i'm making.

saying "if you write your kernel in rust, it will be memory safe" is a little goofy, because writing kernels in rust requires you to drop into unsafe blocks way too often.

Re: Hobby x86 kernel written with Zig

#57
post #51

Earlier quoted context omitted.

As an ancestor in this thread pointed out: it's hard/impossible to go without unsafe in a kernel. Proof (and they have a policy to only use unsafe when not otherwise possible): https://github.com/redox-os/kernel/search?q=unsafe&unscoped_...

yes, that's the point i'm making. saying "if you write your kernel in rust, it will be memory safe" is a little goofy, because writing kernels in rust requires you to drop into unsafe blocks way too often.

Not as often as you would think, Phill Oppermann has shown this: https://os.phil-opp.com/

Unsafe is definitely used, but it does not mean what I think is being implied, which is that it makes the software unsafe.

Re: Hobby x86 kernel written with Zig

#58
post #51

Earlier quoted context omitted.

As an ancestor in this thread pointed out: it's hard/impossible to go without unsafe in a kernel. Proof (and they have a policy to only use unsafe when not otherwise possible): https://github.com/redox-os/kernel/search?q=unsafe&unscoped_...

yes, that's the point i'm making. saying "if you write your kernel in rust, it will be memory safe" is a little goofy, because writing kernels in rust requires you to drop into unsafe blocks way too often.

The ammount of unsafe blocks is pretty small compared to the rest of the code. Which means those parts can be reviewed more extensively. If anything your argument is pretty goofy.

Re: Hobby x86 kernel written with Zig

#59
post #6

Earlier quoted context omitted.

> I used to do this in Rust but I switched to zig for maintainability and readability over Rust. Can you expand on this ? I'm asking out of curiosity because I want to learn a "system" programming language (for whatever definition there is to this term). So far I briefly tried Rust and Nim and found the former more difficult to read. I know nothing about Zig, how would you place it between these two ?

In general you'll find that zig is easier to read than Rust (see the first version of this project in Rust [0]) because it's a simpler language. For kernel programming this is even more so the case: * zig has native support for arbitrary sized integers. In Rust I used to do bitshifts, Now I just have a packed struct of u3/u5/u7 whatever (see `src/pci/pci.zig`). Of course Rust has a bitflags crate but I didn't find it…

> I don't know Nim, but I believe it has a garbage collector so it could be tricky to use for kernel programming.

Have a look at Project Oberon, Singularity, Interlisp-D, Smalltalk, Modula-3 Topaz, Blue Bottle (AOS), CosmosOS, lilith (Crystal variant) given that their source is available.

Re: Hobby x86 kernel written with Zig

#60
post #6

Hi, author here! I've just finished writing the pre-emptive multitasking [0](only round robbin though, nothing fancy). I'm currently writing an ATA driver [1], the idea is to implement ext2. I used to do this in Rust but I switched to zig for maintainability and readability over Rust. It seems that with `comptime` I'm able to make a lot of things optimal. Overall I have to say kernel programming is _hard_ but very re…

> I used to do this in Rust but I switched to zig for maintainability and readability over Rust. Can you expand on this ? I'm asking out of curiosity because I want to learn a "system" programming language (for whatever definition there is to this term). So far I briefly tried Rust and Nim and found the former more difficult to read. I know nothing about Zig, how would you place it between these two ?

I find Zig's approach to generics to be very cool. Types are a first class citizen, so you basically get generics out of the box. Combined with Zig's `comptime` makes for writing very cool code. This solves lot of things you'd need macros in other languages to do.
Post reply on HN