r/lua • u/[deleted] • Nov 11 '20
Luajit Based OS
https://github.com/dlannan/ljos
Im working on building a Luajit based OS. Hence the name. Currently, it happily runs in qemu, and will also boot on my laptop and run as well (from a USB). It is an "in-memory" single user mode kernel at the moment (this will change).
It uses some excellent packages like Luajit, ljsyscall, FBGraphics, kilo, lummander, sdl2, cairo and more. I am currently working on the graphical side of things and getting sdl2 and cairo to operate without issue, as well as integrating my old UI that uses cairo.
Happy to get feedback, suggestions and ideas. Note: This is a "barebones" OS, the full OS will be based around a new style of UI system called mitree.


4
u/beaubeautastic Nov 11 '20
ive thought about a luajit kernel for a really long time. all code could run in ring 0 making system calls very fast, opening up a lot of possibility. could look like what unix was supposed to look like, with the filesystem being just a regular table and giving developers access to the objects themselves.
5
Nov 11 '20
Hi, yes exactly. This has been something Ive been thinking of since I was first introduced to Lua a while back. Ideally, once the basics are working, Id like to not have a filesystem (per se) and rather a simple indexing system like redis or memcache. Redis is preferrable because then clustering, and a number of other features come with the system.
You would still _use_ and program the system normally (fopen etc) but everything will be held in protected structures like redis. It also makes backups, save points, and so on much easier to implement. However.. I am only just at the beginning of this little adventure.
Its been quite fun, it reminds me of coding on the C64 and Amiga again. Where the access and definition of what happens is entirely up to you :)
3
u/hawhill Nov 11 '20
Hm, so it is something like LJ/Linux (c.f. GNU/Linux)? A Linux kernel plus a "LuaJIT userland"? You have listed some packages - and indeed, probably LuaJIT has to be added itself - that I can't imagine running without a libc, so I guess that's still there? If not for other stuff, then at least for heap management? What function fulfills LuaJIT in this - is it what drives the shell? And possibly the init/PID 1 process?
3
Nov 11 '20
Hi, good questions, and yes, Im a bit of an idiot. Luajit should be in that list.
A generic Linux kernel is loaded (AMD64 bit at the moment, but this can be ARM etc). There are a usual set of GNU libs. As time goes on, I will reduce these and end up using things like musl (would prefer if everything was MIT, but not there yet). This runs the kernel process after booting from grub.
The first init/PID 1 process is Luajit as you rightly guessed. The shell is provided mostly by scripts and the great lummander toolkit. Its _very_ rudimentary at the moment. But lummander has an excellent command extension system. Will add tab completion and so on in the near future (its one of the thing that annoys me most at the moment :) ).
Additionally, the future use will be Luajit starting a "process manager" of sorts, that launches and spawns child Luajit's running applications/services etc. This us where FBP will be heavily used.
Hope that explanation helps. Im still working through the "hows" at the moment, and will put some documentation on the github page once I confirm working elements.
Thanks again for the feedback.
2
Nov 11 '20
Additionally, the future use will be Luajit starting a "process manager" of sorts, that launches and spawns child Luajit's running applications/services etc. This us where FBP will be heavily used.
This sounds very interesting; I wonder if you have given thought to the API to use to communicate across such processes. I have found that the love2d API for starting separate LuaJIT instances and communicating with them over channels is simple and very useful: https://love2d.org/wiki/love.thread
Having the text editor be written in C is a bit of a drag; have you considered something like lite instead: https://github.com/rxi/lite/ It has a small C shim (mostly to get drawing primitives) but is mostly written in Lua, and is very extensible.
2
Nov 11 '20
You are quite predictive! I have added lite recently, since as you point out a C based text editor is a little annoying. Id like to get most (if not all) utilities built into the system as lua scripts.
I wrote a FBP kernel engine many years ago: https://github.com/dlannan/deity
This is what will be used to sequence data flows. Its not really IPC, its more of a internal web network that has a fixed interface. And all modules are black box, self contained. The aim is to generate proper code reuse, and proper common OS/System frameworks you only need to code once.
Thanks for the feedback. I have used love2d in the past, and liked it very much. There are many good systems like that out there (maratis, ufo, moai.. etc). I dont expect to have anywhere near similar functionality :)
2
Nov 11 '20
Interesting; thanks.
I have never heard of FBP, but from reading the readme you linked to, it does not look like a concurrency mechanism. Are you planning on having concurrency in your OS? If you do my vote would be for a system based on message-passing queues like love2d which is a lot like an actor-based system; actor-based systems tend to be the most reliable in my experience.
2
Nov 12 '20 edited Nov 12 '20
FBP is more like a development/framework paradigm. You dont write code like you normally would (although with Lua much will be hidden).
Think of it more like functional modules (not too granular) with common interfaces, and the 'programming' is how the data is routed. So all the complexity and development lies in the connected topography, not the interfaces themselves.
As an example I previously built a game engine that allowed realtime switching between DX8 (yes, good old days) and OpenGL. As far as the 'program' was concerned it was just swapping data paths.
Hope that helps explain it a little. It can be a little odd to work with.
Oh, and yes, concurrency is how it all works, although not in the way you would probably recognize it. All the modules are "running" and servicing streaming data requests. The Deity system was hampered by the OS and especially the dll mechanisms - works better as separate processes.
Btw - not sure what you mean about actor based systems? Ie Unreal Engine? or Agent based systems maybe? These are more implementation entity systems which Im not sure I understand the use for process control.
[edit] Just read about the actor model on wiki - not really similar. FBP modules dont have rights over starting/stopping other modules. All they can do is exchange data. Although, that is similar to the messaging interface, so I guess its a kind of hybrid?
2
Nov 12 '20
Thanks for the clarification. So it sounds like FBP can handle concurrency, but it's intended more as an implementation detail? You program in a declarative way, and the system handles orchestrating which bits run on which threads; is that accurate?
Your description reminds me of Functional Reactive Programming, which is only one letter off: https://en.wikipedia.org/wiki/Functional_reactive_programming
1
Nov 13 '20 edited Nov 13 '20
This is better described in the fbp article :)
FBP is a special case of dataflow programming characterized by asynchronous, concurrent processes "under the covers", Information Packets with defined lifetimes, named ports, "bounded buffer" connections, and definition of connections external to the components - it has been found to support improved development time and maintainability, reusability, rapid prototyping, simulation, improved performance, and good communication among developers, maintenance staff, users, systems people, and management - not to mention that FBP naturally takes advantage of multiple cores... without the programmer having to struggle with the intricacies of multitasking!
There are various modes of implementation and styles as well. One group has made a network service system using the methodology:https://noflojs.org/
Hope this helps define it a little more.
[edit]
Just going to expand the idea a bit. Technically the "internet" is a large FB system (of sorts). As are many messaging systems (IPC could be classed as an FB system). So its more of an architectural programming model, than implementation level. However, features like concurrency are automatically provided by design.
Have a play with Paul Morrisson's toolkits - they are interesting to use: https://jpaulm.github.io/fbp/software.html
Or join us on slack :)
2
u/wikipedia_text_bot Nov 12 '20
Functional reactive programming
Functional reactive programming (FRP) is a programming paradigm for reactive programming (asynchronous dataflow programming) using the building blocks of functional programming (e.g. map, reduce, filter). FRP has been used for programming graphical user interfaces (GUIs), robotics, games, and music, aiming to simplify these problems by explicitly modeling time.
2
u/hawhill Nov 11 '20
Thanks for the insight, and no, you're most certainly not a bit of an idiot - your project shows lots of insight into the workings and interwork of Linux kernel and userland and for this alone it certainly has its worth!
3
u/nelsci Nov 11 '20
Great work! When I saw your post, I remembered this paper https://www.netbsd.org/~lneto/dls14.pdf
3
Nov 11 '20
Ahh yes. Classic paper :) .. has definitely been a part inspiration. As well as Justin's amazing work with ljsyscall. Which has really helped things along a huge amount.
https://www.lua.org/wshop13/Cormack.pdf
Thanks for the feedback.
3
Nov 11 '20
Oh. Some notes:
Beware running samples! Some will not exit - no signal handling yet :) (its going in now).
To look at available commands type help.
Use exec to run a real binary, and dofile to run lua scripts. Things will break!
And DONT run the development system outside of qemu unless you really understand what will happen. You _can_ completely break your linux environment if you do the wrong thing. I would always recommend running in qemu.
2
u/Wolf_4501 Nov 11 '20
I suggest you to use yieldhook to possibly create pre-empative multitasking (it will yield running coroutine when the debug hook that attached to the coroutine return true) and i success doing that but i dont know if it will compile for LuaJIT
2
Nov 11 '20 edited Nov 11 '20
when
Thanks, I'll check it out. Ive used debug hooks in the past like debugger.lua (which is pretty awesome). If its clean C, then it should work fine, thanks again for the tip.
[update]
Im not sure I can use this, since its Lua5.3. It might be able to be converted though. Oddly, I am needing something like this at the moment, so thankyou for the suggestion. I'll be investigating more into this package.
2
u/Wolf_4501 Nov 12 '20
you could use luasdl2 for SDL2 access in lua (can be used to create a SDL2 screen that you could use as your LuaJIT OS monitor) that my other suggestion
1
Nov 12 '20
Good call - but Im already using a ffi wrapper for SDL :) .. the trick is getting it to work with dev/fb0 (which has been interesting).
I hope to have a cairo/lite/something graphical start soon. The terminal at the moment is for testing all the parts. My daughter made a quik svg logo today, so hopefully I can get this all working by weekend.
1
2
u/isr786 Nov 12 '20
You mentioned in one of your comments that you have had some exposure to Plan9. In which case, the following might be of interest (assuming you didn't already know about his - perhaps you do!):
Inferno was/is essentially the plan9 os boiled down into a vm. This vm can itself run on baremetal (in which case, it is the OS), or run as an app under osx, linux, etc.
Within inferno, you have access to much of the original plan9 goodness. Everything is a file, everything uses 9p. So you can coble together a distributed system by just mounting /dev/cpu from others, etc). It even has acme! Plus it has a few additions (inferno's shell is highly modular, and can be used a proper scripting language).
Unlike plan9, inferno is implemented in lingo. You can think of lingo as a precursor to go (Rob Pike being involved with both).
Node9 is an (unfortunately stalled) effort to take Inferno, rip out limbo, and replace it with lua. With some interesting ideas on how lua croutines can be used in lieu of processes, etc.
Thought this might be of interest :-)
1
Nov 12 '20
Thankyou. Yes, I have used Inferno (or attempted to). There is also a variant that runs lua as its base scripting system (cant remember the name). The 9p file system is quite awesome though, I like the natural way it works across distributed systems. There are some nice 9p c libraries Id like to try out in the future, but I need to determine how that will integrate with the redis backend. Also, little note, 9p is supported by qemu :) which makes development much nicer too: https://www.linux-kvm.org/page/9p_virtio
For me. Simplicity and flexibility is key. LJOS I hope to grow as something _anyone_ can jump in and just start using. Think C64, Apple, DOS (+GWBasic). Where you, the author, can control the tool (the computer) quickly and easily.
A simple example: I really want to have singular application solutions - there wont be 12 ways to open a text file. Just one. And its built into the OS.
Same with media files, and similar. Where the data is strict, the usage will be also. I dont want confusion on "how to use a data source". This is a little bit over-bearing possibly, but I really want a stable, simple system I can use and rely upon.
Sorry.. about the ideology. :)
1
2
u/Barp_the_Wire Nov 12 '20
Damn this is awesome!
Looks my dreams come true, I will definitely check it out.
2
Nov 12 '20
Could be your nightmares too :) .. it works, but very specific. x64 only and its a fair bit in flux at the moment. Lots of great suggestions coming from here, so thanks for the support.
4
u/WrongAndBeligerent Nov 11 '20
If any of it works, I'm already impressed. Never forget that copying QNX will be cutting edge for at least the next 20 years.
1
u/CockroachOld2279 Apr 30 '26
oh shit im doing the same thing :|
1
Apr 30 '26
Awesome. I should update this a bit 😄 .. has been a long time, and Ive built _alot_ of other things what would now really expand this OS. Maybe in a few weeks I'll add some things.
Id like to add my luajit-sokol project so I can have more interesting capabilities, but Im not sure if I can get that operational on fb0. Im pretty sure the nuklear gui should work.. so that might be first 😄1
u/CockroachOld2279 Apr 30 '26
If you wanted to see https://github.com/ReTylinate/LJOS/
We had the same exact idea
But mine has no graphcs or really anything yet, this one is amazing!1
May 01 '26
Nice Work! Well done!
Ive been lucky enough to have spent time working on a commercial OS that I cant really talk about that uses a lightweight Linux kernel and a userland that has luajit as a processor space (bit hard to describe). And it was _so_ powerful - its why its the leading hardware solution in its space.
What I wanted, like you, was to go one step futher and make the userland _in_ a Luajit env. And with all that lovely ffi driving the "hard stuff". It works pretty well. And Ive recently managed to finish working on my own Luajit based FBP system called neurojit which I hope to put up on github soon - this is intended to be a FBP OS like system for NNs.
Then the aim to get that + my sokol-lujit + dim + nuklear all into this 😄
https://github.com/dlannan/sokol-luajit/
https://github.com/dlannan/dim/All very slow going when I have three or four other projects on the go.. + work.. + family 😄
All good fun tho.2
u/CockroachOld2279 May 01 '26
i usually like to have at most 2 projects active at once. Any more gets overwhelming.
1
May 01 '26
Yeah I can understand. Ive been doing this (coding, electronics, etc) since the mid 80s and my current technique is that when I hit road-blocks or substantial things I need to do that require some deeper thinking or planning, I switch. Thus I end up with alot of projects 😄
But the nice thing is (for me) it often triggers quite lateral solutions to other problems and so the end result is I kinda "dance" around the projects and I dont try to pressure myself into a corner for time because I have my family and work that is a higher priority in life for me (they always get my time regardless of whats happening - esp family).
The other kinda weird side effect of this, is it lets me make "research projects" that dont have to go anywhere, but often end up enhancing other projects ( dim <-> worldbuilder <-sokol for instance).
Alot of my game dev and sim dev friends focus alot on "finishing X" and I think they miss the real value in learning - that is the learning 😄 Finishing is great, but the meaning of that in software is kinda moot - no software is ever finished.
If you are making software for a living this holds true also but thats a bigger discussion.
Have a great day.
2
2
u/CockroachOld2279 May 01 '26
Did you try running it?
1
May 01 '26 edited May 01 '26
I only had a quick look. I'll give it a run this arvo.
And yes, all my stuff is MIT.. do what you want with it! 😉 (except for the obvious Linux GPL bits)2
u/CockroachOld2279 May 01 '26
No idea if it runs on a real computer, I've only tested in Qemu...
1
May 01 '26
Sok will try on Qemu as well.
Can give it a whirl on a usb + laptop too if you want, its pretty easy to do. Will let you know how it goes - prob in a couple of hours time. I have a few things to sort out.
2
u/CockroachOld2279 May 01 '26
I only HAVE a laptop and no pc so my main os is on my laptop too
1
May 01 '26
Got it booting fine 😄 Well done. Nice little Luajit sandpit.
Can make you a bootable iso if you want. I used a simple ultra base x86_64 kernel to launch it. Seemed fine.Made a couple of scripts for making an initramfs (for easy booting) and a qemu launcher. Works well. Can send some PR's if youd like.
→ More replies (0)2
u/CockroachOld2279 May 01 '26
One of the only reasons I'm doing this is that my dominant coding language and really the only one i code well in is Lua
1
May 01 '26
Have you done much in Luajit?
FFI, Coro and jit is kinda why I have stuck with it all these years. I can integrate any Clib I make in a few minutes. Making it a very rapid dev framework.1
u/CockroachOld2279 May 01 '26
ive done a lot in lua and not much in luajit
I like that it's way faster and you can do stuff with it that you can do in Cso yes same as you except i only discovered it last year
2
u/oyo321 Nov 13 '20
I really dislike, that most people believe great companies would keep the informatics sector alive. They don't. In reality coders like you keep the things going! This is impressive! Coders like you have inventive ideas and realize them out of pure fun! Now I am motivated enough to work again on my Love2D project.
1
5
u/piXelicidio Nov 11 '20
Nice! I have a dream of making a 2D game engine purely for Luajit and focused on desktops. Would perfectly fit inside your OS :)
Bookmarked and starred your repo to take look later.