Commit Graph

2927 Commits

Author SHA1 Message Date
Tom
bc107d0b33 Kernel: Add SMP IPI support
We can now properly initialize all processors without
crashing by sending SMP IPI messages to synchronize memory
between processors.

We now initialize the APs once we have the scheduler running.
This is so that we can process IPI messages from the other
cores.

Also rework interrupt handling a bit so that it's more of a
1:1 mapping. We need to allocate non-sharable interrupts for
IPIs.

This also fixes the occasional hang/crash because all
CPUs now synchronize memory with each other.
2020-07-06 17:07:44 +02:00
Andreas Kling
e83c36d053 Kernel: Tidy up SpinLock and related classes a little bit 2020-07-06 11:36:07 +02:00
Andreas Kling
163c9d5f8f Kernel: Thread::wait_on() must always leave interrupts enabled on exit
The short-circuit path added for waiting on a queue that already had a
pending wake was able to return with interrupts disabled, which breaks
the API contract of wait_on() always returning with IF=1.

Fix this by adding a way to override the restored IF in ScopedCritical.
2020-07-06 11:33:32 +02:00
Andreas Kling
3e0020e67d Kernel: Tidy up the ScopedCritical class a little bit 2020-07-06 11:33:32 +02:00
Tom
9725bda63e Kernel: Enhance WaitQueue to remember pending wakes
If WaitQueue::wake_all, WaitQueue::wake_one, or WaitQueue::wake_n
is called but nobody is currently waiting, we should remember that
fact and prevent someone from waiting after such a request. This
solves a race condition where the Finalizer thread is notified
to finalize a thread, but it is not (yet) waiting on this queue.

Fixes #2693
2020-07-06 10:00:24 +02:00
Tom
2a82a25fec Kernel: Various context switch fixes
These changes solve a number of problems with the software
context swithcing:

* The scheduler lock really should be held throughout context switches
* Transitioning from the initial (idle) thread to another needs to
  hold the scheduler lock
* Transitioning from a dying thread to another also needs to hold
  the scheduler lock
* Dying threads cannot necessarily be finalized if they haven't
  switched out of it yet, so flag them as active while a processor
  is running it (the Running state may be switched to Dying while
  it still is actually running)
2020-07-06 10:00:24 +02:00
Tom
49f5069b76 Kernel: Add a SpinLock to the WaitQueue
We need to be able to prevent a WaitQueue from being
modified by another CPU. So, add a SpinLock to it.

Because this pushes some other class over the 64 byte
limit, we also need to add another 128-byte bucket to
the slab allocator.
2020-07-06 10:00:24 +02:00
Tom
788b2d64c6 Kernel: Require a reason to be passed to Thread::wait_on
The Lock class still permits no reason, but for everything else
require a reason to be passed to Thread::wait_on. This makes it
easier to diagnose why a Thread is in Queued state.
2020-07-06 10:00:24 +02:00
Sergey Bugaev
a8489967a3 Kernel: Add Plan9FS :^)
This is an (incomplete, and not very stable) implementation of the client side
of the 9P protocol.
2020-07-05 12:26:27 +02:00
Sergey Bugaev
187b785a05 Kernel: Split BlockBasedFileSystem off FileBackedFileSystem
FileBackedFileSystem is one that's backed by (mounted from) a file, in other
words one that has a "source" of the mount; that doesn't mean it deals in
blocks. The hierarchy now becomes:

* FS
  * ProcFS
  * DevPtsFS
  * TmpFS
  * FileBackedFS
    * (future) Plan9FS
    * BlockBasedFS
      * Ext2FS
2020-07-05 12:26:27 +02:00
Sergey Bugaev
0c72a9eda7 Kernel: Fix .. directory entry at mount point handling a little
It's still broken, but at least it now appears to work if the file system
doesn't return the same inode for "..".
2020-07-05 12:26:27 +02:00
Sergey Bugaev
3645b9e2a6 Kernel: Make sure to drop region with interrupts enabled
A region can drop an inode if it was mmaped from the inode and held the last
reference to it, and that may require some locking.
2020-07-05 12:26:27 +02:00
Sergey Bugaev
6111cfda73 AK: Make Vector::unstable_remove() return the removed value
...and rename it to unstable_take(), to align with other take...() methods.
2020-07-05 12:26:27 +02:00
Sergey Bugaev
63f458ffc1 Kernel: Fix KBufferBuilder::append()
insertion_ptr() already includes the offset.
2020-07-05 12:26:27 +02:00
Andreas Kling
11c4a28660 Kernel: Move headers intended for userspace use into Kernel/API/ 2020-07-04 17:22:23 +02:00
Andreas Kling
0d577ab781 Kernel: Add "child added" and "child removed" InodeWatcher events
The child name is not yet accessible to userspace, but will be in a
future patch.
2020-07-04 13:37:51 +02:00
Andreas Kling
ea17d2d3da Kernel: Remove debug spam in finalizer task 2020-07-04 13:00:48 +02:00
AnotherTest
9609539236 Kernel: Change the value of SO_KEEPALIVE to reflect LibC's constant
This change was partially introduced in 861eb8d, which changed the
constant in LibC without changing the one in the kernel.
2020-07-04 10:49:36 +02:00
Tom
0f2530524e Kernel: Remove /proc/PID/regs
There isn't an easy way to retreive all register contents anymore,
so remove this functionality. We do have the ability to trace
processes, so it shouldn't really be needed anymore.
2020-07-03 21:16:56 +02:00
Tom
bb84fad0bf Kernel: Fix retreiving frame pointer from a thread
If we're trying to walk the stack for another thread, we can
no longer retreive the EBP register from Thread::m_tss. Instead,
we need to look at the top of the kernel stack, because all threads
not currently running were last in kernel mode. Context switches
now always trigger a brief switch to kernel mode, and Thread::m_tss
only is used to save ESP and EIP.

Fixes #2678
2020-07-03 21:16:56 +02:00
Nico Weber
cbbd55bd6b LibC: Remove a few comments now that we have man pages for this. 2020-07-03 19:37:28 +02:00
Tom
9b4e6f6a23 Kernel: Consolidate features into CPUFeature enum
This allows us to consolidate printing out all the CPU features
into one log statement. Also expose them in /proc/cpuinfo
2020-07-03 19:32:34 +02:00
Tom
e373e5f007 Kernel: Fix signal delivery
When delivering urgent signals to the current thread
we need to check if we should be unblocked, and if not
we need to yield to another process.

We also need to make sure that we suppress context switches
during Process::exec() so that we don't clobber the registers
that it sets up (eip mainly) by a context switch. To be able
to do that we add the concept of a critical section, which are
similar to Process::m_in_irq but different in that they can be
requested at any time. Calls to Scheduler::yield and
Scheduler::donate_to will return instantly without triggering
a context switch, but the processor will then asynchronously
trigger a context switch once the critical section is left.
2020-07-03 19:32:34 +02:00
Tom
a308b176ce Kernel: Allow recursion when writing to the debug log
This allows printing in the case e.g. a page fault happens
during a log statement
2020-07-03 19:32:34 +02:00
Tom
3cc0e86cd8 Kernel: Change kmalloc lock to be recursive
If the heap code dumps a stack trace (e.g. out of memory) then
it may recursively call into it. Rather than deadlocking, allow
recursion.
2020-07-03 19:32:34 +02:00
Tom
57b61b2dde Kernel: Split initialization of Processor structure
We need to very early on initialize the Processor structure so
that we can use RecursiveSpinLock early on.
2020-07-03 19:32:34 +02:00
Andreas Kling
a98712035c Kernel: Fix non-blocking write() blocking instead of short-writing
If a partial write succeeded, we could then be in an unexpected state
where the file description was non-blocking, but we could no longer
write to it.

Previously, the kernel would block in that state, but instead we now
handle this as a proper short write and return the number of bytes
we were able to write.

Fixes #2645.
2020-07-03 13:54:18 +02:00
Andreas Kling
47f5b24cc8 Kernel: Remove no-longer-used GDT selector from Thread
Now that we use software context switching, each thread no longer has
its own GDT entry (yay!) so we can get rid of this Thread member. :^)
2020-07-02 21:50:42 +02:00
Emanuele Torre
aabb482d5c Meta: move Kernel/.bochsrc => Meta/bochsrc
The run script is not in Kernel/ anymore, let's move `.bochsrc` in Meta/
so that it can be used with the new build system.

Also make bochs use `grub_disk_image` instead of `_disk_image`
2020-07-01 12:53:39 +02:00
Tom
5674a77bd6 PATA: Ignore interrupts that weren't generated by the disk 2020-07-01 12:07:01 +02:00
Tom
a2fd824dff PATA: LBA48 uses 16 bit features register 2020-07-01 12:07:01 +02:00
Tom
96109e9776 Kernel: Boot all APS all the way into their own idle loop 2020-07-01 12:07:01 +02:00
Tom
691d767fba Kernel: Block initializing the Scheduler on the APs until the BSP initialized global data 2020-07-01 12:07:01 +02:00
Tom
2a38cc9a12 Kernel: Add a quickmap region for each processor
Threads need to be able to concurrently quickmap things.
2020-07-01 12:07:01 +02:00
Tom
d249b5df8f Kernel: Protect Console with SpinLock 2020-07-01 12:07:01 +02:00
Tom
16783bd14d Kernel: Turn Thread::current and Process::current into functions
This allows us to query the current thread and process on a
per processor basis
2020-07-01 12:07:01 +02:00
Tom
d99901660d Kernel/LibCore: Expose processor id where a thread last ran 2020-07-01 12:07:01 +02:00
Tom
d98edb3171 Kernel: List all CPUs in /proc/cpuinfo 2020-07-01 12:07:01 +02:00
Tom
fb41d89384 Kernel: Implement software context switching and Processor structure
Moving certain globals into a new Processor structure for
each CPU allows us to eventually run an instance of the
scheduler on each CPU.
2020-07-01 12:07:01 +02:00
Tom
10407061d2 PATA: Avoid double-preparing for irq 2020-07-01 12:07:01 +02:00
Tom
3ac6d31b45 Kernel: Serialize debug output 2020-07-01 12:07:01 +02:00
Peter Elliott
e1aef94a40 Kernel: Make Random work on CPUs without rdrand
- If rdseed is not available, fallback to rdrand.
- If rdrand is not available, block for entropy, or use insecure prng
  depending on if user wants fast or good random.
2020-06-27 19:40:33 +02:00
3541
4fa6301523 Kernel: Add g_cpu_supports_rdseed
CPUs which support RDRAND do not necessarily support RDSEED. This
introduces a flag g_cpu_supports_rdseed which is set appropriately
by CPUID. This causes Haswell CPUs in particular (and probably a lot
of AMD chips) to now fail to boot with #2634, rather than an illegal
instruction.

It seems like the KernelRng needs either an initial reseed call or
more random events added before the first call to get_good_random,
but I don't feel qualified to make that kind of change.
2020-06-27 12:57:03 +02:00
Peter Elliott
af0b2d1d86 Kernel: Harvest randomness from various drivers
Random now gets entropy from the following drivers:
- KeyboardDevice
- PATAChannel
- PS2MouseDevice
- E1000NetworkAdapter
- RTL8139NetworkAdapter

Of these devices,  PS2MouseDevice and PATAChannel provide the vast
majority of the entropy.
2020-06-25 21:05:40 +02:00
Peter Elliott
2e8cfe5435 LibCrypto: Add CTR cipher mode
Kernel: Changed fortuna implementation to use CTR mode instead of
manually implementing a counter.
2020-06-25 21:05:40 +02:00
Peter Elliott
0f32155fa4 Kernel: Replace existing random implementation with Fortuna 2020-06-25 21:05:40 +02:00
Peter Elliott
f2d51f13a6 Kernel: Implement the Fortuna PRNG algorithm 2020-06-25 21:05:40 +02:00
Sergey Bugaev
6efbbcd4ba Kernel: Port mounts to reference inodes directly
...instead of going through their identifiers. See the previous commit for
reasoning.
2020-06-25 15:49:04 +02:00
Sergey Bugaev
df66c28479 Kernel: Deemphasize inode identifiers
These APIs were clearly modeled after Ext2FS internals, and make perfect sense
in Ext2FS context. The new APIs are more generic, and map better to the
semantics exported to the userspace, where inode identifiers only appear in
stat() and readdir() output, but never in any input.

This will also hopefully reduce the potential for races (see commit c44b4d61f3).

Lastly, this makes it way more viable to implement a filesystem that only
synthesizes its inodes lazily when queried, and destroys them when they are no
longer in use. With inode identifiers being used to reference inodes, the only
choice for such a filesystem is to persist any inode it has given out the
identifier for, because it might be queried at any later time. With direct
references to inodes, the filesystem will know when the last reference is
dropped and the inode can be safely destroyed.
2020-06-25 15:49:04 +02:00
Andreas Kling
28bfd73b68 Kernel: Minor cleanups in sendfd/recvfd
Applying some nice suggestions by @bugaevc. :^)
2020-06-25 10:58:37 +02:00