HackerTrans
TopNewTrendsCommentsPastAskShowJobs

thingfish

no profile record

Submissions

MinC Is Not Cygwin

minc.commandlinerevolution.nl
284 points·by thingfish·작년·228 comments

comments

thingfish
·작년·discuss
Thanks for taking the extra time making me consider dropping the term "kernel". But I need a term wich reflects my efforts. So I fed your description at the end into ChatGPT, asking for a one-word description. This is what it came up with:

Paravirtualized – commonly used when an interface mimics low-level system behavior but redirects to a host implementation; this fits your syscall-level emulation.

Interposed – describes the interception and redirection of system calls or API behavior.

Emulated – broad but accurate; emphasizes imitation of behavior at the syscall layer.

Rehosted – suggests the POSIX environment has been adapted to a different host OS.

Substituted – captures the idea of replacing one system mechanism with another.

Intercepted – emphasizes the capture and redirection of syscalls.

Translated – suitable if there's transformation between POSIX calls and Windows kernel APIs.

Adapted – highlights compatibility through minimal changes.

Bridged – suggests a connection or interface between incompatible systems.

Virtualized – somewhat general, but appropriate if the environment feels like a lightweight VM layer.

Interesting, but not very creative. Something like Ted Nelson's "transclusion". Suggestions? Anyone?
thingfish
·작년·discuss
OpenBSD has pkg_add and friends. Which I can't release yet. They need Perl which I can't release yet either. Although stable already, there still is one vital thing I need implemented in MinC.
thingfish
·작년·discuss
I wrote an entire 3-year (dutch) course around it [0]. My predecessor started his course making them do endless exercises with user rights and vim, which is very boring for 16-year olds. I start them on Apache webserver. This generates immediate interest, and at the same time tricks them into learning about user rights and using vim.

[0] http://commandlinerevolution.nl/Huiswerk/
thingfish
·작년·discuss
My choice for this particular IRC client (not naming it) came from the Mr.Robot series (S04E04 at 59:38:00). I am teaching 16 year olds. They like that kind of thing.
thingfish
·작년·discuss
Thanks for taking the time to read the source code and commenting on it. And you're right. My kernel is implemented in userspace, so I should use the term "kernel emulation". But I did find it fascinating to find out some of the reasons why a kernel is needed in the first place.

For starters, I needed to put the code of my kernel emulation (libposix-6.1.0.dll) at a specific memory address in order to get that snappy feeling when you start an OpenBSD program. Normally, 32bit DLLs in Windows are loaded at 0x10000000 (called the "ImageBase"). This causes the Windows kernel reposition the DLL because in most cases, other DLLs are already loaded there. In my case these are netapi32.dll, ws2_32.dll, iphlpapi.dll and shell32.dll among others. Making my DLLs load at an address just below the 0x10000000 boundary makes MinC programs load faster, in effect creating a space for my kernel, e.g. "kernel space".

Secondly, I needed some mechanism to reset the contents of the CPU's registers after calling into the various Windows DLLs which are not part of the Windows NT kernel. For instance, the WriteFile() function changes the %edx register, which is used by the GCC compiler to contain the high part of 64bit variables. Turns out I needed what is called a "context switch", saving the contents of all registers onto the stack, calling my kernel emulation and then restoring the registers from the stack. In Linux, this mechanism is triggered by a special interrupt (int 0x80). I decided to name the function that does this in my kernel emulation "__kernel", so in assembly I can write "call ___kernel" [0].

There is more, but I won't go into that right now. I plan to do a write-up on the topic. By the way, Microsoft also has a user space implementation of their kernel, called kernel32.dll, implementing Win32 APIs in user space.

[0] https://github.com/dboland/minc/blob/master/libposix/arch/x8...
thingfish
·작년·discuss
Wait, I sense some genuine concern here. In fact, you tricked me into learning, which is what I also do with my students. I've read the article you provided in your earlier comment, plus the one from Microsoft [0].

They expose a dark secret behind the fork() call and I felt it too when implementing it myself. Almost gave me a heart failure. So here's my simplified take: what if the parent did a malloc() and put the resulting pointer on the stack (which is what git does, BTW). A simple copy of the stack to the child wouldn't be sufficient. The kernel then would have to follow the pointer, malloc() new memory and copy the data. It's not hard to see where this is going. What if there is malloc()ed memory in this copy? It's madness. I suspect this could bring a whole system to its knees.

This is a problem a kernel should not try to solve. Only user space knows about its application of memory. While reading the source code of the software I include in MinC, I always thought this is bad programming, perpetuated by Torvalds' #1 rule "don't mess with user-space", leading to things like copy-on-write.

This all leads me to believe I can get away with doing flat copies of stack and globals during fork(), implement spawn(), never implement copy-on-write and patch the userland code if needed. Am I right?

[0] https://www.microsoft.com/en-us/research/wp-content/uploads/...
thingfish
·작년·discuss
> I've to say that you've clearly thought a lot about this, and though I've not tried it (and probably won't) you seem to have done an excellent job.

Thank you. I needed that. I started the project in 2016 and have been obsessed with it since then. The weird thing is that I never had to use workarounds or hacks that are mentioned by the Cygwin team [0]. For instance, the select() call does map cleanly on top of the Win32 API. I just needed to use WSAWaitForMultipleEvents() instead of WaitForMultipleEvents() (the other "easteregg"). Why the Cygwin people didn't figure this out baffles me. I guess their current code base doesn't allow the rewrite. My big breakthrough was when I realized that the "inconsistent interfaces" [1] in Win32 file handles can be implemented as virtual file systems. One for each handle type (char, disk, pipe, etc). That was my "throwing away 1000 lines of code" [2] moment.

As to the weird file names, I use the file names OpenBSD uses. My rule is to always use the file name of the header (.h) file where the system call is declared in OpenBSD. I also use their struct and constant names, prefixed with "WIN_".

The "fork is evil" thing is discussed a lot in the programmers community. I myself find it quite clever. Threads are highly volatile and are very hard to program without running into race conditions. The solution is to make a copy of everything the child will be using: duplicate file descriptors, the stack, globals (rss). The kernel does all this for you in one system call. I often wonder how the people who complain about the absence of real concurrency in their programming languages [3] actually would use this feature. In my opinion the best way to use concurrency is to string individual programs into a pipeline. This will never go "evil" on you.

[0] https://cygwin.com/cygwin-ug-net/highlights.html

[1] https://www.usenix.org/legacy/publications/library/proceedin...

[2] https://skeptics.stackexchange.com/questions/43800/did-the-c...

[3] https://news.ycombinator.com/item?id=32408577
thingfish
·작년·discuss
Cool. You found out why MinC is not Cygwin. To fork(), Cygwin starts a second copy of the entire executable. CreateThread() is much closer to what UNIX does. Any thoughts? BTW, there's another "easteregg".
thingfish
·작년·discuss
I was a limper too, until I saw this demonstration by the creator of PowerShell from 2004: https://www.youtube.com/watch?v=4mBRA7pqITM
thingfish
·작년·discuss
I understand your point, but I'm intrigued. Could you elaborate with some kind of example why it is disappointing that people don't date their web-pages? Is this a generic problem or is it with specific web-sites only?
thingfish
·작년·discuss
Ah! You got it. Are you a teacher? You should try it.
thingfish
·작년·discuss
I don't mind. Many of the comments are about my incorrect use of the term Linux. In a way, they are right. But if one wants to teach Automotive Technology to future BMW mechanics, you start them working on a VW Beetle. Wax on wax off.
thingfish
·작년·discuss
It works now. I had to add -pix_fmt yuv420p to my ffmpeg parameters. Thanks for the comments on this.
thingfish
·작년·discuss
Yes.
thingfish
·작년·discuss
You hit an important point here. Teaching IT to 16 jear old children is very much like being an admin for a large corporation. Most of them have installed VMWare, VirtualBox, Hyper-V and pfSense, depending on the preference of the other teachers. Most first-graders don't know anything about networking, so I didn't want to add WSL to that. I used Cygwin. Worked perfectly until I started to teach Sendmail. Sendmail runs in unprivileged mode. This means that it starts as root, but then switches to a user with very few rights. Cygwin couldn't handle that at first, but I got it working. But then students couldn't uninstall Cygwin anymore because of changed file ownership in Windows. I had to install a second Cygwin to uninstall the first. With MinC I took extreme care to get all the different models of Access Control Lists that are used in Windows right.
thingfish
·작년·discuss
I added an .mp4 source. Still not playing in BlueStacks Android. Does it work in a real Andriod?
thingfish
·작년·discuss
It is a .webm file. It won't play in my BlueStacks Android emulator either. Do I need to add an .mp4 file?
thingfish
·작년·discuss
I started out by creating a dll file with all the system calls implemented. Later I learned how a kernel actually works. So I rewrote it, emulating an interrupt to run in a separate kernel space. Could you name some of the details you want described?
thingfish
·작년·discuss
You're right. I'll change it to "Windows XP or later".
thingfish
·작년·discuss
Yes. Teaching anything to children is to start simple. So I start with bare-bone UNIX. In their second year we move to Linux (all flavours). For their final exam in the third year, they must be able to use Suze Linux.