Open Orycterope opened 5 years ago
Is a mutex just a virtual address for the kernel ?
yes. You are not supposed to use a mutex that's aliased. Very UB, bad idea.
What if the mutex lives in a shared mapping ?
Same, very UB. Mutex is process-local. The tags they use (thread handles) are process-local too, and can conflict in a cross-process scenario. (unlikely, but y'know, it can happen).
We should make sure that it is impossible to create an alias for a mutex in safe rust.
I mean, it should already be impossible because all the functions to create shared mapping from existing memory should be unsafe. If they aren't, that's a problem.
What do we do when the thread owning a mutex crashes ?
An excellent question. First of all, a thread that crashes in unexpected ways is supposed to kill the whole process, so it's not a big issue. But what about a thread that locks a mutex, and then calls svcExitThread()
without unlocking? I spent some time reversing, and I am fairly sure the answer is "nothing happens" still, so yeah, the mutex will stay locked forever. Should probably test on hardware though, just to make sure.
I'm trying to design how the kernel will implement userspace mutex.
Here's a few properties for a mutex, from the kernel's perspective:
lock
andunlock
syscalls, and the kernel uses it as an ID for the mutex, and never interprets it in any way.From there implementing mutex seems quite simple, but I still need to figure out what should be the behavior in some edge-cases: