1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2024-06-30 22:36:23 +03:00
iris/kernel.hhp
2009-08-05 10:16:24 +02:00

259 lines
7.5 KiB
Plaintext

#pypp 0
// Iris: micro-kernel for a capability-based operating system.
// kernel.hhp: Header for all kernel sources.
// Copyright 2009 Bas Wijnen <wijnen@debian.org>
//
// 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 <http://www.gnu.org/licenses/>.
#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 Page
struct Thread
struct Message
struct Receiver
struct Capability
struct Caps
struct Memory
typedef Page *PageP
typedef Thread *ThreadP
typedef Message *MessageP
typedef Receiver *ReceiverP
typedef Capability *CapabilityP
typedef Caps *CapsP
typedef Memory *MemoryP
typedef void *Pointer
typedef unsigned Protected
struct CapRef:
CapsP caps
unsigned index
inline Capability *deref ()
operator bool ():
return deref () != NULL
Capability *operator-> ():
return deref ()
void reset ():
caps = NULL
CapRef (CapsP c, unsigned i) : caps (c), index (i):
CapRef () : caps (NULL), index (~0):
inline void clone (CapRef source, bool copy)
inline void set (Receiver *target, Protected pdata, CapRef parent, CapRef *parent_ptr = NULL)
struct Object:
CapRef refs
MemoryP address_space
// Next and previous object of the same type in any page.
Pointer prev, next
inline bool is_free ()
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::is_free ():
return ((Free *)this)->marker == ~0
// Include architecture-specific parts.
#include "arch.hh"
struct Message : public Object:
Protected protected_data
unsigned data[4]
unsigned char capabilities[4]
struct Capability : public Object:
struct Context:
unsigned data[4]
CapRef cap[4]
bool copy[4]
ReceiverP target
CapRef parent
CapRef children
CapRef sibling_prev, sibling_next
Protected protected_data
void invoke (Context *c)
void invalidate ()
struct Thread : public Object:
ReceiverP receivers
unsigned pc, sp
Thread_arch arch
unsigned flags
ThreadP schedule_prev, schedule_next
CapRef rcaps[4]
// caps is an array of slots pointers to Capses.
unsigned slots
// TODO: handle freeing of capses which are in use.
CapsP caps[1]
void raise (unsigned code, unsigned data)
void run ()
void unrun ()
void wait (CapRef c0, CapRef c1, CapRef c2, CapRef c3)
void unwait ()
bool is_waiting ():
return flags & THREAD_FLAG_WAITING
CapRef find_capability (unsigned code, bool *copy)
struct Receiver : public Object:
ThreadP owner
ReceiverP prev_owned, next_owned
ReceiverP prev_alarm, next_alarm
unsigned alarm_count
// random is used like the tlb random register to find invalid caps to store the message to.
unsigned random
CapsP caps
// These are capabilities which call this receiver on invoke.
CapRef capabilities
// The message queue. Messages are added at the tail, and removed at the front.
MessageP messages
MessageP last_message
Protected reply_protected_data
bool protected_only
void own (ThreadP o)
void orphan ()
bool try_deliver ()
bool send_message (Protected protected_data, Capability::Context *c)
struct Page : public Object:
unsigned frame
unsigned flags
PageP share_first
PageP share_prev, share_next
Page_arch arch
void forget ()
struct Caps : public Object:
unsigned size
Capability caps[1]
Capability *cap (unsigned idx):
return &caps[idx]
void set (unsigned index, Receiver *target, Protected pdata, CapRef parent, CapRef *parent_ptr = NULL)
void clone (unsigned index, CapRef source, bool copy)
struct Memory : public Object:
Free *frees
PageP pages
ThreadP threads
ReceiverP receivers
CapsP capses
MemoryP 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 (unsigned size)
Message *alloc_message (Receiver *target)
Receiver *alloc_receiver ()
Caps *alloc_caps (unsigned size)
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_caps (Caps *page)
void free_memory (Memory *mem)
void free_obj (Object *obj, void **first)
// 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_impl (unsigned n, unsigned line, char const *name, char const *message = "")
EXTERN unsigned dbg_code
EXTERN CapRef dbg_cap
void dbg_log_char (unsigned ch)
void dbg_log (char const *str)
void dbg_log_num (unsigned num)
#define panic(n, m) panic_impl ((n), __LINE__, __PRETTY_FUNCTION__, (m))
/// Defined in schedule.ccp
void schedule ()
void timer_interrupt ()
EXTERN Memory top_memory
EXTERN ReceiverP first_alarm
EXTERN Thread idle
EXTERN Memory idle_memory
EXTERN Page idle_page
EXTERN ThreadP first_scheduled
EXTERN ThreadP current, old_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, Protected protected_data, unsigned *data)
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, ReceiverP 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)
Capability *CapRef::deref ():
return caps ? caps->cap (index) : NULL
void CapRef::clone (CapRef source, bool copy):
caps->clone (index, source, copy)
void CapRef::set (Receiver *target, Protected pdata, CapRef parent, CapRef *parent_ptr):
caps->set (index, target, pdata, parent, parent_ptr)
#define assert(x) do { if (!(x)) panic (__LINE__, "assertion failed"); } while (0)
#endif