Commit graph

54 commits

Author SHA1 Message Date
Ben Gras 2d72cbec41 SYSENTER/SYSCALL support
. add cpufeature detection of both
	. use it for both ipc and kernelcall traps, using a register
	  for call number
	. SYSENTER/SYSCALL does not save any context, therefore userland
	  has to save it
	. to accomodate multiple kernel entry/exit types, the entry
	  type is recorded in the process struct. hitherto all types
	  were interrupt (soft int, exception, hard int); now SYSENTER/SYSCALL
	  is new, with the difference that context is not fully restored
	  from proc struct when running the process again. this can't be
	  done as some information is missing.
	. complication: cases in which the kernel has to fully change
	  process context (i.e. sigreturn). in that case the exit type
	  is changed from SYSENTER/SYSEXIT to soft-int (i.e. iret) and
	  context is fully restored from the proc struct. this does mean
	  the PC and SP must change, as the sysenter/sysexit userland code
	  will otherwise try to restore its own context. this is true in the
	  sigreturn case.
	. override all usage by setting libc_ipc=1
2012-09-24 15:53:43 +02:00
David van Moolenbroek 8e116b71a1 Kernel: resolve Coverity warnings 2012-08-15 11:12:11 +00:00
David van Moolenbroek cf9a4ec79b Kernel: clean up include statements a bit
Coverity was flagging a recursive include between kernel.h and
cpulocals.h. As cpulocals.h also included proc.h, we can move that
include statement into kernel.h, and clean up the source files'
include statements accordingly.
2012-08-14 16:29:05 +00:00
Ben Gras 1d48c0148e segmentless smp fixes
adjust the smp booting procedure for segmentless operation. changes are
mostly due to gdt/idt being dependent on paging, because of the high
location, and paging being on much sooner because of that too.

also smaller fixes: redefine DESC_SIZE, fix kernel makefile variable name
(crosscompiling), some null pointer checks that trap now because of a
sparser pagetable, acpi sanity checking
2012-07-15 22:47:20 +02:00
Ben Gras 50e2064049 No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.

There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.

No static pre-allocated memory sizes exist any more.

Changes to booting:
        . The pre_init.c leaves the kernel and modules exactly as
          they were left by the bootloader in physical memory
        . The kernel starts running using physical addressing,
          loaded at a fixed location given in its linker script by the
          bootloader.  All code and data in this phase are linked to
          this fixed low location.
        . It makes a bootstrap pagetable to map itself to a
          fixed high location (also in linker script) and jumps to
          the high address. All code and data then use this high addressing.
        . All code/data symbols linked at the low addresses is prefixed by
          an objcopy step with __k_unpaged_*, so that that code cannot
          reference highly-linked symbols (which aren't valid yet) or vice
          versa (symbols that aren't valid any more).
        . The two addressing modes are separated in the linker script by
          collecting the unpaged_*.o objects and linking them with low
          addresses, and linking the rest high. Some objects are linked
          twice, once low and once high.
        . The bootstrap phase passes a lot of information (e.g. free memory
          list, physical location of the modules, etc.) using the kinfo
          struct.
        . After this bootstrap the low-linked part is freed.
        . The kernel maps in VM into the bootstrap page table so that VM can
          begin executing. Its first job is to make page tables for all other
          boot processes. So VM runs before RS, and RS gets a fully dynamic,
          VM-managed address space. VM gets its privilege info from RS as usual
          but that happens after RS starts running.
        . Both the kernel loading VM and VM organizing boot processes happen
	  using the libexec logic. This removes the last reason for VM to
	  still know much about exec() and vm/exec.c is gone.

Further Implementation:
        . All segments are based at 0 and have a 4 GB limit.
        . The kernel is mapped in at the top of the virtual address
          space so as not to constrain the user processes.
        . Processes do not use segments from the LDT at all; there are
          no segments in the LDT any more, so no LLDT is needed.
        . The Minix segments T/D/S are gone and so none of the
          user-space or in-kernel copy functions use them. The copy
          functions use a process endpoint of NONE to realize it's
          a physical address, virtual otherwise.
        . The umap call only makes sense to translate a virtual address
          to a physical address now.
        . Segments-related calls like newmap and alloc_segments are gone.
        . All segments-related translation in VM is gone (vir2map etc).
        . Initialization in VM is simpler as no moving around is necessary.
        . VM and all other boot processes can be linked wherever they wish
          and will be mapped in at the right location by the kernel and VM
          respectively.

Other changes:
        . The multiboot code is less special: it does not use mb_print
          for its diagnostics any more but uses printf() as normal, saving
          the output into the diagnostics buffer, only printing to the
          screen using the direct print functions if a panic() occurs.
        . The multiboot code uses the flexible 'free memory map list'
          style to receive the list of free memory if available.
        . The kernel determines the memory layout of the processes to
          a degree: it tells VM where the kernel starts and ends and
          where the kernel wants the top of the process to be. VM then
          uses this entire range, i.e. the stack is right at the top,
          and mmap()ped bits of memory are placed below that downwards,
          and the break grows upwards.

Other Consequences:
        . Every process gets its own page table as address spaces
          can't be separated any more by segments.
        . As all segments are 0-based, there is no distinction between
          virtual and linear addresses, nor between userspace and
          kernel addresses.
        . Less work is done when context switching, leading to a net
          performance increase. (8% faster on my machine for 'make servers'.)
	. The layout and configuration of the GDT makes sysenter and syscall
	  possible.
2012-07-15 22:30:15 +02:00
Tomas Hruby 5ab87a6c38 ioapic - missing volatiles
leads to a kernel panic when using clang and SMP

reported by trd <trdempsey.201201@gmail.com>
2012-03-29 00:53:38 -07:00
Ben Gras 7336a67dfe retire PUBLIC, PRIVATE and FORWARD 2012-03-25 21:58:14 +02:00
Ben Gras 6a73e85ad1 retire _PROTOTYPE
. only good for obsolete K&R support
	. also remove a stray ansi.h and the proto cmd
2012-03-25 16:17:10 +02:00
Tomas Hruby 5c0927e108 SMP - clock calibration spurious IRQ deadlock fix
- this patch fixes a deadlock which may occur if we get a
   spurious interrupt while calibrating clocks during the boot
   time. Since we never handle interrupts while in the kernel
   (BKL locked) the interrupt code locks the lock. This is a
   different situation, a corner case, boot time only. We do not
   return to userspace but to the kernel, so the BKL is not
   unlocked. So we need irq handler which leaves the BKL
   unlocked.  The clock handler does it already, this patch adds
   a dummy spurious irq handler for the same reason. It is better
   to handle the situation this way to keep the normal runtime
   code simple.
2012-01-26 11:39:40 +00:00
Arun Thomas ae561b8f12 Add MKAPIC and MKACPI options 2011-07-31 16:22:43 +02:00
Arun Thomas 1a8cf59d04 Add MKWATCHDOG option 2011-07-29 20:37:39 +02:00
Arun Thomas c356e9997e kernel: fix GCC warnings 2011-07-18 19:44:59 +02:00
Ben Gras a77c2973b3 fix clang warnings -R in kernel/ and servers/ 2011-06-09 16:09:13 +02:00
Tomas Hruby dc8ee363db SMP - INIT IPI deasserting fix
When deasserting the INIT IPI the DM field must be zero
2011-04-13 16:57:43 +00:00
Arun Thomas aaaad89244 Use int64 functions consistently
Instead of manipulating the u64_t type directly, use the
ex64hi()/ex64lo()/make64() functions.
2010-11-07 23:35:29 +00:00
Tomas Hruby c9bfb13cdb Kernel keeps information about each cpu
- kernel maintains a cpu_info array which contains various
  information about each cpu as filled when each cpu boots

- the information contains idetification, features etc.
2010-10-26 21:07:27 +00:00
Tomas Hruby 98c93e76d7 Zero no more hardwired as BSP apic id
- the BSP apic id is written in the io apic redirection entries to
  deliver the interrupts to BSP
2010-10-21 17:07:07 +00:00
Tomas Hruby a1eefc013e single shot timer interrupts fix
- accidentaly this wasn't part of the SMP merge and the implementation
  remained uncomplete with the timer keeping ticking periodically

- APIC timer is set for a signel shot and restarted everytime it
  expires. This way we can keep the AP's trully idle

- the timer is restarted a little later before leaving to userspace

- LAPIC_TIMER_ICR is written before LAPIC_LVTTR so the newest value is
  used
2010-10-21 17:07:01 +00:00
Tomas Hruby ebbc730fc3 spurious and error interrupt apic handlers
- fixed spurious and error interrupt handlers

- not to hog the system the warning isn't reported every time, just
  once every 100 times, similarly for the spurious PIC interrupts
2010-10-19 17:07:21 +00:00
Tomas Hruby f42b90806a BSP apic id
- BSP apic id used uninitialized causes problems
2010-10-19 17:07:19 +00:00
Tomas Hruby 72cc01ff48 apic_timer_x
- set the apic_timer_x factor variable to slowdown apic timer in
  virtual machines
2010-09-16 07:18:47 +00:00
Tomas Hruby e2701da5a9 SMP - Single shot local timer
- APIC timer always reprogrammed if expired

- timer tick never happens when in kernel => never immediate return
  from userspace to kernel because of a buffered interrupt

- renamed argument to lapic_set_timer_one_shot()

- removed arch_ prefix from timer functions
2010-09-15 14:11:06 +00:00
Tomas Hruby 0ac9b6d4cf SMP - trully idle APs
- any cpu can use smp_schedule() to tell another cpu to reschedule

- if an AP is idle, it turns off timer as there is nothing to
  preempt, no need to wakeup just to go back to sleep again

- if a cpu makes a process runnable on an idle cpu, it must wake it up
  to reschedule
2010-09-15 14:10:57 +00:00
Tomas Hruby 387e1835d1 SMP - BSP halts APs before shutting down 2010-09-15 14:10:54 +00:00
Tomas Hruby 9e12630d75 SMP - APs are fully enabled
- apic_send_ipi() to send inter-processor interrupts (IPIs)

- APIC IPI schedule and halt handlers to signal x-cpu that a cpu shold
  reschedule or halt

- various little changes to let APs run

- no processes are scheduled at the APs and therefore they are idle
  except being interrupted by a timer time to time
2010-09-15 14:10:30 +00:00
Tomas Hruby 9b6d66c787 SMP - BSP waits until the APs finish their booting
- APs configure local timers

- while configuring local APIC timer the CPUs fiddle with the interrupt
  handlers. As the interrupt table is shared the BSP must not run
2010-09-15 14:10:12 +00:00
Tomas Hruby b7aed08e65 SMP - Only a single APIC timer handler
- bsp_timer_int_handler() and ap_timer_int_handler() unified into
  timer_int_handler()

- global realtime updated only on BSP
2010-09-15 14:10:09 +00:00
Tomas Hruby 85cca7096f SMP - The slave CPUs turn paging on
- APs wait until BSP turns paging on, it is not possible to safely
  execute any code on APs until we can turn paging on as well as it
  must be done synchronously everywhere

- APs turn paging on but do not continue and wait
2010-09-15 14:10:07 +00:00
Tomas Hruby 6aa26565e6 SMP - Big kernel lock (BKL)
- to isolate execution inside kernel we use a big kernel lock
  implemented as a spinlock

- the lock is acquired asap after entering kernel mode and released as
  late as possible. Only one CPU as a time can execute the core kernel
  code

- measurement son real hw show that the overhead of this lock is close
  to 0% of kernel time for the currnet system

- the overhead of this lock may be as high as 45% of kernel time in
  virtual machines depending on the ratio between physical CPUs
  available and emulated CPUs. The performance degradation is
  significant
2010-09-15 14:10:03 +00:00
Tomas Hruby 62c666566e SMP - We boot APs
- kernel detects CPUs by searching ACPI tables for local apic nodes

- each CPU has its own TSS that points to its own stack. All cpus boot
  on the same boot stack (in sequence) but switch to its private stack
  as soon as they can.

- final booting code in main() placed in bsp_finish_booting() which is
  executed only after the BSP switches to its final stack

- apic functions to send startup interrupts

- assembler functions to handle CPU features not needed for single cpu
  mode like memory barries, HT detection etc.

- new files kernel/smp.[ch], kernel/arch/i386/arch_smp.c and
  kernel/arch/i386/include/arch_smp.h

- 16-bit trampoline code for the APs. It is executed by each AP after
  receiving startup IPIs it brings up the CPUs to 32bit mode and let
  them spin in an infinite loop so they don't do any damage.

- implementation of kernel spinlock

- CONFIG_SMP and CONFIG_MAX_CPUS set by the build system
2010-09-15 14:09:52 +00:00
Tomas Hruby 6c3b981cd6 arch proto.h renamed to arch_proto.h
- the file moved to the arch include dir
2010-09-15 14:09:36 +00:00
Tomas Hruby e6ebac015d APIC mode uses IO APICs
- kernel turns on IO APICs if no_apic is _not_ set or is equal 0

- pci driver must use the acpi driver to setup IRQ routing otherwise
  the system cannot work correctly except systems like KVM that use
  only legacy (E)ISA IRQs 0-15
2010-09-07 07:18:11 +00:00
Ben Gras f6f814cb02 include, kernel: minor fixes to make compiling and linking work with clang.
(fixing warnings)
2010-07-06 11:59:19 +00:00
Kees van Reeuwijk 826b9590f2 More endpoint_t correctness.
More const correctness.
Other code cleanup.
2010-06-08 14:09:18 +00:00
Kees van Reeuwijk ed0b81c25c Removed some unused variables and functions. 2010-06-02 19:41:38 +00:00
Tomas Hruby 451a6890d6 scheduling - time quantum in miliseconds
- Currently the cpu time quantum is timer-ticks based. Thus the
  remaining quantum is decreased only if the processes is interrupted
  by a timer tick. As processes block a lot this typically does not
  happen for normal user processes. Also the quantum depends on the
  frequency of the timer.

- This change makes the quantum miliseconds based. Internally the
  miliseconds are translated into cpu cycles. Everytime userspace
  execution is interrupted by kernel the cycles just consumed by the
  current process are deducted from the remaining quantum.

- It makes the quantum system timer frequency independent.

- The boot processes quantum is loosely derived from the tick-based
  quantas and 60Hz timer and subject to future change

- the 64bit arithmetics is a little ugly, will be changes once we have
  compiler support for 64bit integers (soon)
2010-05-25 08:06:14 +00:00
Kees van Reeuwijk d106968d77 Remove useless symbol declarations from headers, make symbols local where possible, add some explicit initialization to global variables. 2010-04-22 07:49:40 +00:00
Kees van Reeuwijk 94a81c840a Removed unused variables, added const where possible. 2010-04-07 11:25:51 +00:00
Arun Thomas 4ed3a0cf3a Convert kernel over to bsdmake 2010-04-01 22:22:33 +00:00
Kees van Reeuwijk 4865e3f4f9 More use of endpoint_t. Other code cleanup. 2010-03-30 14:07:15 +00:00
Kees van Reeuwijk 98493805fd Lots of const correctness. 2010-03-27 14:31:00 +00:00
Tomas Hruby 8451a86f0a Interrupts hadling while idle
- When the cpu halts, the interrupts are enable so the cpu may be
  woken up. When the interrupt handler returns but another interrupt
  is available it is also serviced immediately. This is not a problem
  per-se. It only slightly breaks time accounting as idle accounted is
  for the kernel time in the interrupt handler.
  
  
-  As the big kernel lock is lock/unlocked in the smp branch in the
   time acounting functions as they are called exactly at the places
   we need to take the lock) this leads to a deadlock.

- we make sure that once the interrupt handler returns from the nested
  trap, the interrupts are disabled. This means that only one
  interrupt is serviced after idle is interrupted.

- this requires the loop in apic timer calibration to keep reenabling
  the interrupts. I admit it is a little bit hackish (one line),
  however, this code is a stupid corner case at the boot time.
  Hopefully it does not matter too much.
2010-03-23 13:35:01 +00:00
Kees van Reeuwijk c33102ea6b Miscellaneous code cleanup. 2010-03-22 20:43:06 +00:00
Ben Gras e6cb76a2e2 no more kprintf - kernel uses libsys printf now, only kputc is special
to the kernel.
2010-03-03 15:45:01 +00:00
Tomas Hruby 391fd926ff TASK_PRIVILEGE and level0() removed
- there are no tasks running, we don't need TASK_PRIVILEGE priviledge anymore

- as there is no ring 1 anymore, there is no need for level0() to call sensitive
  code from ring 1 in ring 0

- 286 related macros removed as clean up
2010-02-09 15:23:31 +00:00
Tomas Hruby 728f0f0c49 Removal of the system task
* Userspace change to use the new kernel calls

	- _taskcall(SYSTASK...) changed to _kernel_call(...)

	- int 32 reused for the kernel calls

	- _do_kernel_call() to make the trap to kernel

	- kernel_call() to make the actuall kernel call from C using
	  _do_kernel_call()

	- unlike ipc call the kernel call always succeeds as kernel is
	  always available, however, kernel may return an error

* Kernel side implementation of kernel calls

	- the SYSTEm task does not run, only the proc table entry is
	  preserved

	- every data_copy(SYSTEM is no data_copy(KERNEL

	- "locking" is an empty operation now as everything runs in
	  kernel

	- sys_task() is replaced by kernel_call() which copies the
	  message into kernel, dispatches the call to its handler and
	  finishes by either copying the results back to userspace (if
	  need be) or by suspending the process because of VM

	- suspended processes are later made runnable once the memory
	  issue is resolved, picked up by the scheduler and only at
	  this time the call is resumed (in fact restarted) which does
	  not need to copy the message from userspace as the message
	  is already saved in the process structure.

	- no ned for the vmrestart queue, the scheduler will restart
	  the system calls

	- no special case in do_vmctl(), all requests remove the
	  RTS_VMREQUEST flag
2010-02-09 15:20:09 +00:00
Tomas Hruby b14a86ca5c Sys calls are called ipc calls now
- the syscalls are pretty much just ipc calls, however, sendrec() is
  used to implement system task (sys) calls

- sendrec() won't be used anymore for this, therefore ipc calls will
  become pure ipc calls
2010-02-09 15:13:07 +00:00
Kees van Reeuwijk b67f788eea Removed a number of useless #includes 2010-01-26 10:59:01 +00:00
Kees van Reeuwijk a7cee5bec4 Removed unused symbols.
Minor cleanups.
2010-01-22 22:01:08 +00:00
Tomas Hruby 5efa92f754 NMI watchdog is an awesome feature for debugging locked up kernels.
There is not that much use for it on a single CPU, however, deadlock
between kernel and system task can be delected. Or a runaway loop.

If a kernel gets locked up the timer interrupts don't occure (as all
interrupts are disabled in kernel mode). The only chance is to
interrupt the kernel by a non-maskable interrupt.

This patch generates NMIs using performance counters. It uses the most
widely available performace counters. As the performance counters are 
highly model-specific this patch is not guaranteed to work on every
machine.  Unfortunately this is also true for KVM :-/ On the other
hand adding this feature for other models is not extremely difficult
and the framework makes it hopefully easy enough.

Depending on the frequency of the CPU an NMI is generated at most
about every 0.5s If the cpu's speed is less then 2Ghz it is generated
at most every 1s. In general an NMI is generated much less often as
the performance counter counts down only if the cpu is not idle.
Therefore the overhead of this feature is fairly minimal even if the
load is high.

Uppon detecting that the kernel is locked up the kernel dumps the 
state of the kernel registers and panics.

Local APIC must be enabled for the watchdog to work.

The code is _always_ compiled in, however, it is only enabled if  
watchdog=<non-zero> is set in the boot monitor.

One corner case is serial console debugging. As dumping a lot of stuff
to the serial link may take a lot of time, the watchdog does not 
detect lockups during this time!!! as it would result in too many
false positives. 10 nmi have to be handled before the lockup is
detected. This means something between ~5s to 10s.

Another corner case is that the watchdog is enabled only after the
paging is enabled as it would be pure madness to try to get it right.
2010-01-16 20:53:55 +00:00