2009-05-11 01:28:12 +03:00
|
|
|
#pypp 0
|
2009-06-01 15:26:42 +03:00
|
|
|
// 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/>.
|
|
|
|
|
2009-05-11 01:28:12 +03:00
|
|
|
#ifndef _KERNEL_HH
|
|
|
|
#define _KERNEL_HH
|
|
|
|
|
2009-06-01 15:26:42 +03:00
|
|
|
// Include definitions which are shared with user space.
|
2009-05-25 01:31:35 +03:00
|
|
|
#define __KERNEL
|
2009-06-01 15:26:42 +03:00
|
|
|
#include "iris.h"
|
2009-05-22 23:48:49 +03:00
|
|
|
|
2009-06-01 15:26:42 +03:00
|
|
|
// 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.
|
2009-05-18 10:30:27 +03:00
|
|
|
#ifndef EXTERN
|
|
|
|
#define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct Page
|
|
|
|
struct Thread
|
2009-05-20 23:07:56 +03:00
|
|
|
struct Message
|
|
|
|
struct Receiver
|
|
|
|
struct Capability
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Caps
|
2009-05-18 10:30:27 +03:00
|
|
|
struct Memory
|
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
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
|
2009-05-18 10:30:27 +03:00
|
|
|
// Next and previous object of the same type in any page.
|
2009-08-05 11:16:24 +03:00
|
|
|
Pointer prev, next
|
|
|
|
inline bool is_free ()
|
2009-05-18 10:30:27 +03:00
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Free : public Object:
|
2009-05-18 10:30:27 +03:00
|
|
|
// This marker is ~0. No other kernel structure may allow this value
|
|
|
|
// at this point. It is used to recognize free chunks.
|
|
|
|
unsigned marker
|
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
bool Object::is_free ():
|
2009-05-18 10:30:27 +03:00
|
|
|
return ((Free *)this)->marker == ~0
|
|
|
|
|
2009-06-01 15:26:42 +03:00
|
|
|
// Include architecture-specific parts.
|
2009-05-26 01:42:47 +03:00
|
|
|
#include "arch.hh"
|
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Message : public Object:
|
|
|
|
Protected protected_data
|
2009-05-20 23:07:56 +03:00
|
|
|
unsigned data[4]
|
2009-08-05 11:16:24 +03:00
|
|
|
unsigned char capabilities[4]
|
2009-05-20 23:07:56 +03:00
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Capability : public Object:
|
2009-06-08 14:46:13 +03:00
|
|
|
struct Context:
|
|
|
|
unsigned data[4]
|
2009-08-05 11:16:24 +03:00
|
|
|
CapRef cap[4]
|
2009-06-08 14:46:13 +03:00
|
|
|
bool copy[4]
|
2009-08-05 11:16:24 +03:00
|
|
|
ReceiverP target
|
|
|
|
CapRef parent
|
|
|
|
CapRef children
|
|
|
|
CapRef sibling_prev, sibling_next
|
|
|
|
Protected protected_data
|
2009-07-20 01:23:45 +03:00
|
|
|
void invoke (Context *c)
|
2009-06-08 14:46:13 +03:00
|
|
|
void invalidate ()
|
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Thread : public Object:
|
|
|
|
ReceiverP receivers
|
2009-07-25 01:54:12 +03:00
|
|
|
unsigned pc, sp
|
|
|
|
Thread_arch arch
|
|
|
|
unsigned flags
|
2009-08-05 11:16:24 +03:00
|
|
|
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]
|
2009-07-25 01:54:12 +03:00
|
|
|
void raise (unsigned code, unsigned data)
|
|
|
|
void run ()
|
|
|
|
void unrun ()
|
2009-08-05 11:16:24 +03:00
|
|
|
void wait (CapRef c0, CapRef c1, CapRef c2, CapRef c3)
|
2009-07-25 01:54:12 +03:00
|
|
|
void unwait ()
|
|
|
|
bool is_waiting ():
|
|
|
|
return flags & THREAD_FLAG_WAITING
|
2009-08-05 11:16:24 +03:00
|
|
|
CapRef find_capability (unsigned code, bool *copy)
|
2009-07-25 01:54:12 +03:00
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Receiver : public Object:
|
|
|
|
ThreadP owner
|
|
|
|
ReceiverP prev_owned, next_owned
|
|
|
|
ReceiverP prev_alarm, next_alarm
|
2009-07-20 01:23:45 +03:00
|
|
|
unsigned alarm_count
|
2009-08-05 11:16:24 +03:00
|
|
|
// 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
|
2009-06-01 02:12:54 +03:00
|
|
|
bool protected_only
|
2009-08-05 11:16:24 +03:00
|
|
|
void own (ThreadP o)
|
2009-05-24 13:22:22 +03:00
|
|
|
void orphan ()
|
2009-06-01 02:12:54 +03:00
|
|
|
bool try_deliver ()
|
2009-08-05 11:16:24 +03:00
|
|
|
bool send_message (Protected protected_data, Capability::Context *c)
|
2009-05-11 01:28:12 +03:00
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Page : public Object:
|
2009-05-29 00:35:27 +03:00
|
|
|
unsigned frame
|
|
|
|
unsigned flags
|
2009-08-05 11:16:24 +03:00
|
|
|
PageP share_first
|
|
|
|
PageP share_prev, share_next
|
2009-05-29 00:35:27 +03:00
|
|
|
Page_arch arch
|
2009-06-01 02:12:54 +03:00
|
|
|
void forget ()
|
2009-05-29 00:35:27 +03:00
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Caps : public Object:
|
|
|
|
unsigned size
|
|
|
|
Capability caps[1]
|
2009-07-20 01:23:45 +03:00
|
|
|
Capability *cap (unsigned idx):
|
2009-08-05 11:16:24 +03:00
|
|
|
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)
|
2009-05-24 13:22:22 +03:00
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
struct Memory : public Object:
|
2009-05-18 10:30:27 +03:00
|
|
|
Free *frees
|
2009-08-05 11:16:24 +03:00
|
|
|
PageP pages
|
|
|
|
ThreadP threads
|
|
|
|
ReceiverP receivers
|
|
|
|
CapsP capses
|
|
|
|
MemoryP memories
|
2009-05-11 01:28:12 +03:00
|
|
|
unsigned limit, used
|
2009-05-18 10:30:27 +03:00
|
|
|
Memory_arch arch
|
2009-05-11 01:28:12 +03:00
|
|
|
|
2009-05-20 23:07:56 +03:00
|
|
|
inline bool map (Page *page, unsigned address, bool write)
|
|
|
|
inline void unmap (Page *page, unsigned address)
|
2009-05-24 13:22:22 +03:00
|
|
|
inline Page *get_mapping (unsigned address, bool *writable)
|
2009-05-19 00:18:23 +03:00
|
|
|
|
2009-05-18 10:30:27 +03:00
|
|
|
// Allocation of pages.
|
2009-07-20 01:23:45 +03:00
|
|
|
bool use (unsigned num = 1)
|
|
|
|
void unuse (unsigned num = 1)
|
2009-05-22 23:48:49 +03:00
|
|
|
unsigned palloc ()
|
|
|
|
unsigned zalloc ()
|
|
|
|
void pfree (unsigned page)
|
|
|
|
void zfree (unsigned page)
|
2009-05-11 01:28:12 +03:00
|
|
|
|
2009-05-18 10:30:27 +03:00
|
|
|
// Allocation routines for kernel structures
|
2009-05-19 00:18:23 +03:00
|
|
|
void *search_free (unsigned size, void **first)
|
2009-05-18 10:30:27 +03:00
|
|
|
Page *alloc_page ()
|
2009-08-05 11:16:24 +03:00
|
|
|
Thread *alloc_thread (unsigned size)
|
2009-07-20 01:23:45 +03:00
|
|
|
Message *alloc_message (Receiver *target)
|
2009-05-20 23:07:56 +03:00
|
|
|
Receiver *alloc_receiver ()
|
2009-08-05 11:16:24 +03:00
|
|
|
Caps *alloc_caps (unsigned size)
|
2009-05-18 10:30:27 +03:00
|
|
|
Memory *alloc_memory ()
|
2009-05-11 01:28:12 +03:00
|
|
|
|
2009-05-19 00:18:23 +03:00
|
|
|
void free_page (Page *page)
|
|
|
|
void free_thread (Thread *thread)
|
2009-07-20 01:23:45 +03:00
|
|
|
void free_message (Receiver *owner, Message *message)
|
2009-05-20 23:07:56 +03:00
|
|
|
void free_receiver (Receiver *receiver)
|
2009-08-05 11:16:24 +03:00
|
|
|
void free_caps (Caps *page)
|
2009-05-19 00:18:23 +03:00
|
|
|
void free_memory (Memory *mem)
|
|
|
|
|
2009-08-05 11:16:24 +03:00
|
|
|
void free_obj (Object *obj, void **first)
|
2009-05-19 00:18:23 +03:00
|
|
|
|
2009-05-18 10:30:27 +03:00
|
|
|
// Functions which can be called from assembly must not be mangled.
|
|
|
|
extern "C":
|
2009-05-11 01:28:12 +03:00
|
|
|
// Panic. n is sent over caps led. message is currently ignored.
|
2009-07-25 01:54:12 +03:00
|
|
|
void panic_impl (unsigned n, unsigned line, char const *name, char const *message = "")
|
2009-07-20 01:23:45 +03:00
|
|
|
EXTERN unsigned dbg_code
|
2009-08-05 11:16:24 +03:00
|
|
|
EXTERN CapRef dbg_cap
|
2009-07-23 13:06:32 +03:00
|
|
|
void dbg_log_char (unsigned ch)
|
|
|
|
void dbg_log (char const *str)
|
2009-07-25 01:54:12 +03:00
|
|
|
void dbg_log_num (unsigned num)
|
|
|
|
|
|
|
|
#define panic(n, m) panic_impl ((n), __LINE__, __PRETTY_FUNCTION__, (m))
|
2009-05-11 01:28:12 +03:00
|
|
|
|
2009-07-04 17:21:28 +03:00
|
|
|
/// Defined in schedule.ccp
|
2009-05-20 23:07:56 +03:00
|
|
|
void schedule ()
|
2009-07-04 17:21:28 +03:00
|
|
|
void timer_interrupt ()
|
2009-05-20 23:07:56 +03:00
|
|
|
|
2009-05-11 01:28:12 +03:00
|
|
|
EXTERN Memory top_memory
|
2009-08-05 11:16:24 +03:00
|
|
|
EXTERN ReceiverP first_alarm
|
2009-05-11 01:28:12 +03:00
|
|
|
EXTERN Thread idle
|
|
|
|
EXTERN Memory idle_memory
|
|
|
|
EXTERN Page idle_page
|
2009-08-05 11:16:24 +03:00
|
|
|
EXTERN ThreadP first_scheduled
|
|
|
|
EXTERN ThreadP current, old_current
|
2009-07-20 01:23:45 +03:00
|
|
|
EXTERN bool do_schedule
|
2009-05-20 23:07:56 +03:00
|
|
|
|
2009-07-16 10:15:15 +03:00
|
|
|
// Defined in memory.cc
|
|
|
|
unsigned init_memory (unsigned mem)
|
2009-06-01 02:12:54 +03:00
|
|
|
unsigned raw_zalloc ()
|
|
|
|
void raw_pfree (unsigned page)
|
2009-07-20 01:23:45 +03:00
|
|
|
unsigned phys_alloc (unsigned num)
|
|
|
|
void phys_free (unsigned page, unsigned num)
|
2009-06-01 02:12:54 +03:00
|
|
|
|
2009-06-01 15:26:42 +03:00
|
|
|
// Defined by architecture-specific files.
|
2009-05-20 23:07:56 +03:00
|
|
|
void Thread_arch_init (Thread *thread)
|
2009-08-05 11:16:24 +03:00
|
|
|
void Thread_arch_receive (Thread *thread, Protected protected_data, unsigned *data)
|
2009-05-27 19:33:05 +03:00
|
|
|
unsigned *Thread_arch_info (Thread *thread, unsigned num)
|
2009-05-20 23:07:56 +03:00
|
|
|
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)
|
2009-05-24 13:22:22 +03:00
|
|
|
Page *Memory_arch_get_mapping (Memory *mem, unsigned address, bool *writable)
|
2009-05-29 00:35:27 +03:00
|
|
|
void Page_arch_update_mapping (Page *page)
|
2009-08-05 11:16:24 +03:00
|
|
|
void arch_register_interrupt (unsigned num, ReceiverP r)
|
2009-05-20 23:07:56 +03:00
|
|
|
|
|
|
|
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)
|
2009-05-24 13:22:22 +03:00
|
|
|
Page *Memory::get_mapping (unsigned address, bool *writable):
|
|
|
|
return Memory_arch_get_mapping (this, address, writable)
|
2009-08-05 11:16:24 +03:00
|
|
|
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)
|
2009-05-11 01:28:12 +03:00
|
|
|
|
2009-07-20 01:23:45 +03:00
|
|
|
#define assert(x) do { if (!(x)) panic (__LINE__, "assertion failed"); } while (0)
|
|
|
|
|
2009-05-11 01:28:12 +03:00
|
|
|
#endif
|