#pypp 0 // Iris: micro-kernel for a capability-based operating system. // kernel.hhp: Header for all kernel sources. // Copyright 2009 Bas Wijnen // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #ifndef _KERNEL_HH #define _KERNEL_HH // Include definitions which are shared with user space. #define __KERNEL #include "iris.h" // Normally define all variables in this file as extern. // Exactly once (in data.ccp), EXTERN is predefined empty. // That results in space being allocated in its object file. #ifndef EXTERN #define EXTERN extern #endif struct Object_base struct Page struct Thread struct Message struct Receiver struct Capability struct Cappage struct Memory struct Object_base: Capability *refs Memory *address_space inline bool is_free () template // struct Object : public Object_base: // Next and previous object of the same type in any page. _T *prev, *next struct Free : public Object : // This marker is ~0. No other kernel structure may allow this value // at this point. It is used to recognize free chunks. unsigned marker bool Object_base::is_free (): return ((Free *)this)->marker == ~0 // Include architecture-specific parts. #include "arch.hh" struct Thread : public Object : Receiver *receivers unsigned pc, sp Thread_arch arch unsigned flags Thread *schedule_prev, *schedule_next void run () void unrun () void wait () void unwait () bool is_waiting (): return flags & THREAD_FLAG_WAITING struct Message : public Object : Capability *capabilities[4] unsigned data[4] unsigned protected_data struct Capability : public Object : struct Context: unsigned data[4] Capability *cap[4] bool copy[4] Receiver *target Capability *parent Capability *children Capability *sibling_prev, *sibling_next unsigned protected_data void invoke (Context *c) void invalidate () struct Receiver : public Object : Thread *owner Receiver *prev_owned, *next_owned Receiver *prev_alarm, *next_alarm unsigned alarm_count Capability *capabilities Message *messages Message *last_message unsigned reply_protected_data bool protected_only void own (Thread *o) void orphan () bool try_deliver () bool send_message (unsigned protected_data, Capability::Context *c) struct ShareData : unsigned frame unsigned flags void *share_first void *share_prev, *share_next struct Page : public Object : ShareData data Page_arch arch void forget () struct Cappage : public Object : ShareData data Capability *cap (unsigned idx): return &((Capability *)data.frame)[idx] void forget () struct Memory : public Object : Free *frees Page *pages Thread *threads Receiver *receivers Capability *capabilities Cappage *cappages Memory *memories unsigned limit, used Memory_arch arch inline bool map (Page *page, unsigned address, bool write) inline void unmap (Page *page, unsigned address) inline Page *get_mapping (unsigned address, bool *writable) // Allocation of pages. bool use (unsigned num = 1) void unuse (unsigned num = 1) unsigned palloc () unsigned zalloc () void pfree (unsigned page) void zfree (unsigned page) // Allocation routines for kernel structures void *search_free (unsigned size, void **first) Page *alloc_page () Thread *alloc_thread () Message *alloc_message (Receiver *target) Receiver *alloc_receiver () Capability *alloc_capability (Receiver *target, Capability *parent, Capability **parent_ptr, unsigned protected_data, Capability *ret = NULL) Capability *clone_capability (Capability *source, bool copy, Capability *ret = NULL) Cappage *alloc_cappage () Memory *alloc_memory () void free_page (Page *page) void free_thread (Thread *thread) void free_message (Receiver *owner, Message *message) void free_receiver (Receiver *receiver) void free_capability (Capability *capability) void free_cappage (Cappage *page) void free_memory (Memory *mem) void free_obj (Object_base *obj) Capability *find_capability (unsigned code, bool *copy) // Functions which can be called from assembly must not be mangled. extern "C": // Panic. n is sent over caps led. message is currently ignored. void panic (unsigned n, char const *message = "") // Debug: switch caps led void dbg_led (bool one, bool two, bool three) void dbg_sleep (unsigned ms) void dbg_send (unsigned code, unsigned bits = 32) EXTERN unsigned dbg_code EXTERN Capability *dbg_cap void dbg_log_char (unsigned ch) void dbg_log (char const *str) /// Defined in schedule.ccp void schedule () // Make sure the current process is no longer scheduled. This is called just before wait (), if the current process is the target. void unschedule () void timer_interrupt () EXTERN Memory top_memory EXTERN Receiver *first_alarm EXTERN Thread idle EXTERN Memory idle_memory EXTERN Page idle_page EXTERN Thread *first_scheduled EXTERN Thread *current EXTERN bool do_schedule // Defined in memory.cc unsigned init_memory (unsigned mem) unsigned raw_zalloc () void raw_pfree (unsigned page) unsigned phys_alloc (unsigned num) void phys_free (unsigned page, unsigned num) // Defined by architecture-specific files. void Thread_arch_init (Thread *thread) void Thread_arch_receive (Thread *thread, unsigned protected_data, Capability::Context *c) unsigned *Thread_arch_info (Thread *thread, unsigned num) void Memory_arch_init (Memory *mem) void Memory_arch_free (Memory *mem) bool Memory_arch_map (Memory *mem, Page *page, unsigned address, bool write) void Memory_arch_unmap (Memory *mem, Page *page, unsigned address) Page *Memory_arch_get_mapping (Memory *mem, unsigned address, bool *writable) void Page_arch_update_mapping (Page *page) void arch_register_interrupt (unsigned num, Receiver *r) bool Memory::map (Page *page, unsigned address, bool write): return Memory_arch_map (this, page, address, write) void Memory::unmap (Page *page, unsigned address): Memory_arch_unmap (this, page, address) Page *Memory::get_mapping (unsigned address, bool *writable): return Memory_arch_get_mapping (this, address, writable) #define assert(x) do { if (!(x)) panic (__LINE__, "assertion failed"); } while (0) #endif