2009-06-22 17:13:00 +03:00
#pypp 0
2009-06-24 01:47:13 +03:00
// Iris: micro-kernel for a capability-based operating system.
// boot-programs/devices.hhp: interfaces for core devices.
// 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-06-22 17:13:00 +03:00
#ifndef __IRIS_DEVICES_HH
#define __IRIS_DEVICES_HH
2009-08-18 00:11:15 +03:00
#include "iris.hh"
2009-07-27 21:03:58 +03:00
2010-05-01 00:13:49 +03:00
namespace Iris:
// Caplist interface.
template <typename _T> //
2010-05-15 19:42:17 +03:00
struct Caplist : public Caps:
Caplist (Caps c = Cap ()) : Caps (c):
Caplist <_T> create (unsigned size, Memory mem = my_memory):
2010-05-01 00:13:49 +03:00
return Caplist <_T> (mem.create_caps (size))
void set (unsigned idx, _T value):
2010-05-15 19:42:17 +03:00
return Caps::set (idx, value)
2010-05-01 00:13:49 +03:00
_T get (unsigned idx):
2010-05-15 19:42:17 +03:00
return _T (Caps::get (idx))
2009-07-27 21:03:58 +03:00
2010-09-02 00:27:14 +03:00
struct _Locker_base:
enum request:
LOCK_RO = 0x2001
UNLOCK_RO
NUM
template <typename _T> //
struct Locker : public _Locker_base, public _T:
// Lock the object for reading. Multiple read locks can exist simultaneously, but no write lock can be present.
void lock_ro ():
_T::call (CAP_MASTER_DIRECT | LOCK_RO)
// Release a read-only lock.
void unlock_ro ():
_T::call (CAP_MASTER_DIRECT | UNLOCK_RO)
Locker ():
Locker (Cap c) : _T (c):
struct _WLocker_base:
enum request:
LOCK = _Locker_base::NUM
UNLOCK
NUM
template <typename _T> //
struct WLocker : public _WLocker_base, public _T:
// Lock the object. Write operations can only be done when the object is locked.
void lock ():
_T::call (CAP_MASTER_DIRECT | LOCK)
// Unlock the object. Write operations can only be done when the object is locked.
void unlock ():
_T::call (CAP_MASTER_DIRECT | UNLOCK)
WLocker ():
WLocker (Cap c) : _T (c):
/// A short block of data with a size and content. Any character can be stored in it (including '\0').
struct _String : public Cap:
_String (Cap c = Cap ()) : Cap (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-09-02 00:27:14 +03:00
GET_SIZE = _WLocker_base::NUM
2010-05-01 00:13:49 +03:00
GET_CHARS
ID
/// Get the size of the string.
2010-05-15 19:42:17 +03:00
Num get_size ():
2010-05-01 00:13:49 +03:00
return call (CAP_MASTER_DIRECT | GET_SIZE)
2010-09-02 00:27:14 +03:00
/// Get exactly 16 characters. There is no alignment requirement.
2010-05-15 19:42:17 +03:00
char *get_chars (Num idx, char buffer[16]):
2010-05-01 00:13:49 +03:00
call (CAP_MASTER_DIRECT | GET_CHARS, idx)
unsigned *b = (unsigned *)buffer
2010-05-15 19:42:17 +03:00
b[0] = recv.data[0].l
b[1] = recv.data[0].h
b[2] = recv.data[1].l
b[3] = recv.data[1].h
2010-05-01 00:13:49 +03:00
return buffer
2010-09-02 00:27:14 +03:00
typedef Locker <_String> String
/// A block of data with a size and content. Any character can be stored in it (including '\0').
struct _Block : public Cap:
_Block (Cap c = Cap ()) : Cap (c):
enum request:
GET_SIZE = _String::ID
GET_ALIGN_BITS
GET_BLOCK
ID
/// Get the size of the block.
Num get_size ():
return call (CAP_MASTER_DIRECT | GET_SIZE)
2010-06-07 00:03:25 +03:00
/// Get the number of bits that page accesses must be aligned to. Cannot be more than PAGE_BITS.
unsigned get_align_bits ():
return call (CAP_MASTER_DIRECT | GET_ALIGN_BITS).l
/// Helper function for get_block.
2010-05-15 19:42:17 +03:00
static Page _create_paying_page ():
Page ret = my_memory.create_page ()
2010-06-22 21:55:02 +03:00
ret.set_flags (Page::PAYING)
2010-05-01 00:13:49 +03:00
return ret
2010-09-02 00:27:14 +03:00
/// Get a block from the block; place it at offset on page. This need not be implemented for blocks smaller than PAGE_SIZE. All arguments must be aligned. If size is PAGE_SIZE, the caller may lose the frame in the transaction.
2010-06-07 00:03:25 +03:00
Cap get_block (Num idx, unsigned size = PAGE_SIZE, unsigned offset = 0, Page ret = _create_paying_page ()):
ocall (ret, Iris::Num (CAP_MASTER_DIRECT | GET_BLOCK, size << 16 | offset), idx)
2010-05-01 00:13:49 +03:00
return ret
2010-09-02 00:27:14 +03:00
typedef Locker <_Block> Block
2009-09-12 23:48:47 +03:00
2010-05-01 00:13:49 +03:00
/// A writable String.
2010-09-02 00:27:14 +03:00
struct _WString : public String:
_WString (Cap c = Cap ()) : String (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-09-02 00:27:14 +03:00
TRUNCATE = _Block::ID
2010-05-01 00:13:49 +03:00
SET_CHARS
ID
/// Set the size of the string. Strings may have a limit to this setting.
2010-05-15 19:42:17 +03:00
void truncate (Num size):
2010-05-01 00:13:49 +03:00
call (CAP_MASTER_DIRECT | TRUNCATE, size)
2010-09-02 00:27:14 +03:00
/// Set exactly 4 characters. If the size is shorter than idx + 4, the extra characters are ignored.
2010-05-15 19:42:17 +03:00
void set_chars (Num idx, char buffer[4]):
call (Num (CAP_MASTER_DIRECT | SET_CHARS, *(unsigned *)buffer), idx)
2010-09-02 00:27:14 +03:00
typedef WLocker <_WString> WString
/// A writable Block.
struct _WBlock : public Block:
_WBlock (Cap c = Cap ()) : Block (c):
enum request:
TRUNCATE = _Block::ID
SET_BLOCK
ID
/// Set the size of the block. This setting may have a limited range, or not be supported at all.
void truncate (Num size):
call (CAP_MASTER_DIRECT | TRUNCATE, size)
/// Overwrite a block from the block with data at offset on the page. All arguments must be aligned. If size is PAGE_SIZE, the caller may lose the frame in the transaction. Only the specified part of the page is used for overwriting data.
2010-06-07 00:03:25 +03:00
void set_block (Num idx, Page page, unsigned size = PAGE_SIZE, unsigned offset = 0):
ocall (page, Iris::Num (CAP_MASTER_DIRECT | SET_BLOCK, size << 16 | offset), idx)
2010-09-02 00:27:14 +03:00
typedef WLocker <_WBlock> WBlock
2009-07-27 21:03:58 +03:00
2010-08-24 00:55:51 +03:00
// This interface allows another kernel to be booted by Iris.
struct Boot : public Cap:
Boot (Cap c = Cap ()) : Cap (c):
enum request:
2010-09-02 00:27:14 +03:00
BOOT = _WBlock::ID
2010-08-24 00:55:51 +03:00
ID
// Boot a new kernel.
void boot (String code, unsigned load, unsigned entry):
ocall (code, CAP_MASTER_DIRECT | BOOT, Iris::Num (load, entry))
2010-05-01 00:13:49 +03:00
// Every process which wants to be switchable through a terminal must implement this interface.
2010-05-15 19:42:17 +03:00
struct Device : public Cap:
Device (Cap c = Cap ()) : Cap (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-08-24 00:55:51 +03:00
RESET = Boot::ID
2010-05-01 00:13:49 +03:00
ID
2010-05-05 02:09:32 +03:00
// Reset the device. This is called by the terminal while switching owners.
void reset ():
call (CAP_MASTER_DIRECT | RESET)
2009-10-04 20:47:20 +03:00
2010-05-15 19:42:17 +03:00
struct Event : public Device:
Event (Cap c = Cap ()) : Device (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-05-15 19:42:17 +03:00
SET_CB = Device::ID
ID
// Set the event callback. Pending event emit to the new callback immediately.
void set_cb (Cap cb):
ocall (cb, CAP_MASTER_DIRECT | SET_CB)
struct Elfrun : public Cap:
Elfrun (Cap c = Cap ()) : Cap (c):
enum request:
2010-09-02 00:27:14 +03:00
RUN_BLOCK = Event::ID
2010-05-01 00:13:49 +03:00
RUN_CAPS
ID
enum arg_pos:
PARENT_MEMORY
DATA
PARENT
2010-09-02 00:27:14 +03:00
Caps run_block (Memory parent_memory, Block data, Cap parent, unsigned num_slots = 8, unsigned num_caps = 32):
2010-05-15 19:42:17 +03:00
Caps caps = my_memory.create_caps (3)
2010-05-01 00:13:49 +03:00
caps.set (PARENT_MEMORY, parent_memory)
caps.set (DATA, data)
caps.set (PARENT, parent)
2010-09-02 00:27:14 +03:00
iocall (caps.copy (), CAP_MASTER_DIRECT | RUN_BLOCK, Num (num_slots, num_caps))
2010-05-15 19:42:17 +03:00
Caps ret = get_arg ()
my_memory.destroy (caps)
free_cap (caps)
2010-05-01 00:13:49 +03:00
return ret
2010-05-15 19:42:17 +03:00
Caps run_caps (Memory parent_memory, Caps data, Cap parent, unsigned pages, unsigned num_slots = 8, unsigned num_caps = 32):
Caps caps = my_memory.create_caps (3)
2010-05-01 00:13:49 +03:00
caps.set (PARENT_MEMORY, parent_memory)
caps.set (DATA, data)
caps.set (PARENT, parent)
2010-05-15 19:42:17 +03:00
iocall (caps.copy (), Num (CAP_MASTER_DIRECT | RUN_CAPS, pages), Num (num_slots, num_caps))
Caps ret = get_arg ()
my_memory.destroy (caps)
free_cap (caps)
2010-05-01 00:13:49 +03:00
return ret
2009-10-31 10:32:23 +02:00
2010-05-01 00:13:49 +03:00
// Interface for talking to the parent process.
2010-05-15 19:42:17 +03:00
struct Parent : public Cap:
Parent (Cap c = Cap ()) : Cap (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-05-10 02:07:17 +03:00
GET_CAPABILITY = Elfrun::ID
PROVIDE_CAPABILITY
2010-05-01 00:13:49 +03:00
WAIT
GET_MEMORY
PROVIDE_MEMORY
INIT_DONE
EXIT
ID
2010-05-10 02:07:17 +03:00
// Get a handle.
template <typename _T> _T get_capability (unsigned num = 0):
2010-05-15 19:42:17 +03:00
icall (Num (CAP_MASTER_DIRECT | GET_CAPABILITY, num), _T::ID)
return get_arg ()
2010-05-01 00:13:49 +03:00
// Provide a device handle.
2010-05-10 02:07:17 +03:00
template <typename _T> void provide_capability (Cap cap, unsigned num = 0):
2010-05-15 19:42:17 +03:00
ocall (cap, Num (CAP_MASTER_DIRECT | PROVIDE_CAPABILITY, num), _T::ID)
2010-05-01 00:13:49 +03:00
// Wait until a device is used by the caller again.
template <typename _T> void wait (unsigned num = 0):
2010-05-15 19:42:17 +03:00
call (Num (CAP_MASTER_DIRECT | WAIT, num), _T::ID)
2010-05-01 00:13:49 +03:00
// Get memory paid for by another thread, which cannot be inspected or changed by that thread. The memory can be inspected and changed by the user (owning both threads). The call will fail when the threads are not owned by the same user.
2010-05-15 19:42:17 +03:00
Memory get_memory (Cap target):
2010-05-01 00:13:49 +03:00
iocall (target, CAP_MASTER_DIRECT | GET_MEMORY)
2010-05-15 19:42:17 +03:00
return get_arg ()
2010-05-01 00:13:49 +03:00
// Get a handle that another thread can use to call get_memory on. The actual limit on the created memory is floor(limit, thread address space limit).
2010-05-15 19:42:17 +03:00
Cap provide_memory (unsigned limit):
2010-05-01 00:13:49 +03:00
icall (CAP_MASTER_DIRECT | PROVIDE_MEMORY, limit)
2010-05-15 19:42:17 +03:00
return get_arg ()
2010-05-01 00:13:49 +03:00
// Signal the parent that the initialisation phase is over.
2010-05-15 19:42:17 +03:00
void init_done (Num stage = 0):
2010-05-01 00:13:49 +03:00
call (CAP_MASTER_DIRECT | INIT_DONE, stage)
// Exit the program. The parent does not reply, but kills the process.
2010-05-15 19:42:17 +03:00
void exit (Num code):
2010-05-01 00:13:49 +03:00
call (CAP_MASTER_DIRECT | EXIT, code)
2009-07-27 21:03:58 +03:00
2010-05-01 00:13:49 +03:00
// Keyboard interface.
2010-05-15 19:42:17 +03:00
struct Keyboard : public Event:
Keyboard (Cap c = Cap ()) : Event (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-05-15 19:42:17 +03:00
GET_NUM_KEYS = Parent::ID
2010-05-01 00:13:49 +03:00
GET_KEYS
ID
// At event: the callback is called with a keycode. One bit defines if it's a press or release event.
enum constant:
RELEASE = 1 << 31
// Get the number of keys on the keyboard.
unsigned get_num_keys ():
return call (CAP_MASTER_DIRECT | GET_NUM_KEYS).l
// Get the keycodes for the keys. The reply sends 4 key codes (32 bit each).
void get_keys (unsigned first):
call (CAP_MASTER_DIRECT | GET_KEYS, first)
2009-10-04 20:47:20 +03:00
2010-05-01 00:13:49 +03:00
// Buzzer interface.
2010-05-05 02:09:32 +03:00
struct Buzzer : public Device:
2010-05-15 19:42:17 +03:00
Buzzer (Cap c = Cap ()) : Device (c):
2010-05-01 00:13:49 +03:00
enum request:
BEEP = Keyboard::ID
STOP
ID
// Emit a beep of specified frequency, time and volume. Volume may not be supported. If an other beep is in progress, it is aborted.
2010-05-15 19:42:17 +03:00
void beep (unsigned freq, unsigned ms, unsigned volume, Cap cb = Cap ()):
ocall (cb, Num (CAP_MASTER_DIRECT | BEEP, volume), Num (freq, ms))
2010-05-01 00:13:49 +03:00
// Abort current beep, if any.
void stop ():
call (CAP_MASTER_DIRECT | STOP)
2009-07-27 21:03:58 +03:00
2010-05-01 00:13:49 +03:00
// Display interface.
2010-05-05 02:09:32 +03:00
struct Display : public Device:
2010-05-15 19:42:17 +03:00
Display (Cap c = Cap ()) : Device (c):
2010-05-01 00:13:49 +03:00
enum request:
SET_EOF_CB = Buzzer::ID
MAP_FB
2010-05-10 02:07:17 +03:00
UNMAP_FB
2010-05-01 00:13:49 +03:00
GET_INFO
ID
// Register an end-of-frame callback.
// At end of frame, the callback is invoked and forgotten. It must be reregistered to keep a stream of events.
2010-05-15 19:42:17 +03:00
void set_eof_cb (Cap cb):
2010-05-01 00:13:49 +03:00
ocall (cb, CAP_MASTER_DIRECT | SET_EOF_CB)
// Map the framebuffer into memory.
2010-05-15 19:42:17 +03:00
Caps map_fb (unsigned address, Memory mem = my_memory, bool use = true):
iocall (mem, Num (CAP_MASTER_DIRECT | MAP_FB, use ? 1 : 0), address)
return get_arg ()
void unmap_fb (Caps caps):
2010-05-10 02:07:17 +03:00
ocall (caps, CAP_MASTER_DIRECT | UNMAP_FB)
2010-05-01 00:13:49 +03:00
// Get information about the display.
void get_info ():
// TODO: Interface is to be designed.
2010-05-15 19:42:17 +03:00
panic (0, "using undefined interface Display::get_info ()")
2009-07-27 21:03:58 +03:00
2010-05-01 00:13:49 +03:00
// Numerical setting, such as a display backlight.
2010-05-15 19:42:17 +03:00
struct Setting : public Device:
Setting (Cap c = Cap ()) : Device (c):
2010-05-01 00:13:49 +03:00
enum request:
SET = Display::ID
GET_RANGE
ID
// Set a new value.
void set (unsigned value):
call (CAP_MASTER_DIRECT | SET, value)
// Get the maximum value for this setting. Using a higher value with SET gives undefined results.
unsigned get_range ():
return call (CAP_MASTER_DIRECT | GET_RANGE).l
2009-07-27 21:03:58 +03:00
2010-05-01 00:13:49 +03:00
// File system interface.
// filesystem-related interfaces: directory, stream, seekable.
// Normal files implement stream and/or seekable. Directories implement directory.
// Seekable is not a class, it is identical to [W]String.
2009-07-27 21:03:58 +03:00
2010-05-01 00:13:49 +03:00
// Directory interface.
2010-09-02 00:27:14 +03:00
struct _Directory : public Cap:
_Directory (Cap c = Cap ()) : Cap (c):
2010-05-01 00:13:49 +03:00
enum request:
GET_SIZE = Setting::ID
GET_NAME
GET_FILE_RO
GET_FILE_INFO
ID
// Get the number of entries in this directory.
2010-05-15 19:42:17 +03:00
Num get_size ():
2010-05-01 00:13:49 +03:00
return call (CAP_MASTER_DIRECT | GET_SIZE)
// Get the filename.
2010-05-15 19:42:17 +03:00
String get_name (Num idx):
2010-05-01 00:13:49 +03:00
icall (CAP_MASTER_DIRECT | GET_NAME, idx)
2010-05-15 19:42:17 +03:00
return get_arg ()
2010-05-01 00:13:49 +03:00
// Get the file.
2010-05-15 19:42:17 +03:00
Cap get_file_ro (Num idx):
2010-05-01 00:13:49 +03:00
icall (CAP_MASTER_DIRECT | GET_FILE_RO, idx)
2010-05-15 19:42:17 +03:00
return get_arg ()
2010-05-01 00:13:49 +03:00
// Get file info. TODO: define possible types.
2010-05-15 19:42:17 +03:00
Num get_file_info (Num idx, unsigned type):
return icall (Num (CAP_MASTER_DIRECT | GET_FILE_INFO, type), idx)
2010-09-02 00:27:14 +03:00
typedef Locker <_Directory> Directory
struct _WDirectory : public Directory:
_WDirectory (Cap c = Cap ()) : Directory (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-09-02 00:27:14 +03:00
GET_FILE = _Directory::ID
2010-05-01 00:13:49 +03:00
CREATE_FILE
DELETE_FILE
ID
// Get the file.
2010-05-15 19:42:17 +03:00
Cap get_file (Num idx):
2010-05-01 00:13:49 +03:00
icall (CAP_MASTER_DIRECT | GET_FILE, idx)
2010-05-15 19:42:17 +03:00
return get_arg ()
2010-05-01 00:13:49 +03:00
// Create a new file. After this, any index may map to a different file.
2010-05-15 19:42:17 +03:00
Cap create_file (String name):
2010-05-01 00:13:49 +03:00
icall (CAP_MASTER_DIRECT | CREATE_FILE)
2010-05-15 19:42:17 +03:00
return get_arg ()
2010-05-01 00:13:49 +03:00
// Delete a file. After this, any index may map to a different file.
2010-05-15 19:42:17 +03:00
void delete_file (Num idx):
2010-05-01 00:13:49 +03:00
call (CAP_MASTER_DIRECT | DELETE_FILE, idx)
2010-09-02 00:27:14 +03:00
typedef WLocker <_WDirectory> WDirectory
2010-01-14 19:14:37 +02:00
2010-05-01 00:13:49 +03:00
// Stream interface.
2010-05-15 19:42:17 +03:00
struct Stream : public Cap:
Stream (Cap c = Cap ()) : Cap (c):
2010-05-01 00:13:49 +03:00
enum request:
2010-09-02 00:27:14 +03:00
READ = _WDirectory::ID
2010-05-01 00:13:49 +03:00
WRITE
ID
// Try to read size bytes. Returns the number of bytes successfully read.
2010-05-15 19:42:17 +03:00
Num read (Num size, bool block):
return icall (Num (CAP_MASTER_DIRECT | READ, block), size)
2010-05-01 00:13:49 +03:00
// Try to write size bytes. Returns the number of bytes successfully written.
2010-05-15 19:42:17 +03:00
Num write (String s, Num size):
2010-05-01 00:13:49 +03:00
return ocall (s, CAP_MASTER_DIRECT | WRITE, size)
2009-07-27 21:03:58 +03:00
2010-05-15 19:42:17 +03:00
struct UI : public Cap:
UI (Cap c = Cap ()) : Cap (c):
2010-05-10 02:07:17 +03:00
enum request:
GET_STATE = Stream::ID
EVENT
EXIT
ID
enum constant:
INPUT = 1 << 31
2010-05-15 19:42:17 +03:00
void get_state (Cap cap):
2010-05-10 02:07:17 +03:00
ocall (cap, CAP_MASTER_DIRECT | GET_STATE)
2010-07-10 21:14:54 +03:00
void event (unsigned code, Iris::Num value = 0):
2010-05-15 19:42:17 +03:00
call (Num (CAP_MASTER_DIRECT | EVENT, code), value)
2010-05-10 02:07:17 +03:00
void exit ():
call (CAP_MASTER_DIRECT | EXIT)
2010-05-01 00:13:49 +03:00
2009-07-27 21:03:58 +03:00
// TODO.
// Sound interface.
// Usb interfaces (port, device).
// Pointer interface. (Only movement; buttons follow keyboard interface.)
// Network interface.
// Camera interface.
2009-06-22 17:13:00 +03:00
#endif