#pypp 0 // Iris: micro-kernel for a capability-based operating system. // boot-programs/devices.hhp: interfaces for core devices. // 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 __IRIS_DEVICES_HH #define __IRIS_DEVICES_HH #include "iris.hh" namespace Iris: // Caplist interface. template // struct Caplist : public Caps: Caplist (Caps c = Cap ()) : Caps (c): Caplist <_T> create (unsigned size, Memory mem = my_memory): return Caplist <_T> (mem.create_caps (size)) void set (unsigned idx, _T value): return Caps::set (idx, value) _T get (unsigned idx): return _T (Caps::get (idx)) /// A 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): enum request: GET_SIZE = 0x2001 GET_CHARS GET_ALIGN_BITS GET_BLOCK ID /// Get the size of the string. Num get_size (): return call (CAP_MASTER_DIRECT | GET_SIZE) /// Get exactly 16 characters. The index must be aligned to 16 bytes or align_bits, whichever is smaller. char *get_chars (Num idx, char buffer[16]): call (CAP_MASTER_DIRECT | GET_CHARS, idx) unsigned *b = (unsigned *)buffer b[0] = recv.data[0].l b[1] = recv.data[0].h b[2] = recv.data[1].l b[3] = recv.data[1].h return buffer /// 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. static Page _create_paying_page (): Page ret = my_memory.create_page () ret.set_flags (Page::PAYING, Page::PAYING) return ret /// Get a block from the string; place it at offset on page. This need not be implemented for strings smaller than PAGE_SIZE. All arguments must be aligned. 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) return ret /// A writable String. struct WString : public String: WString (Cap c = Cap ()) : String (c): enum request: TRUNCATE = String::ID SET_CHARS SET_BLOCK ID /// Set the size of the string. Strings may have a limit to this setting. void truncate (Num size): call (CAP_MASTER_DIRECT | TRUNCATE, size) /// Set exactly 4 characters. The index must be word-aligned. void set_chars (Num idx, char buffer[4]): call (Num (CAP_MASTER_DIRECT | SET_CHARS, *(unsigned *)buffer), idx) /// Overwrite a block from the string with data at offset on the page. This need not be implemented for strings smaller than PAGE_SIZE. The all arguments must be aligned. The caller may lose the frame in the transaction. Only the specified part of the page is used for overwriting data. 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) // Every process which wants to be switchable through a terminal must implement this interface. struct Device : public Cap: Device (Cap c = Cap ()) : Cap (c): enum request: RESET = WString::ID ID // Reset the device. This is called by the terminal while switching owners. void reset (): call (CAP_MASTER_DIRECT | RESET) struct Event : public Device: Event (Cap c = Cap ()) : Device (c): enum request: 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: RUN_STRING = Event::ID RUN_CAPS ID enum arg_pos: PARENT_MEMORY DATA PARENT Caps run_string (Memory parent_memory, String data, Cap parent, unsigned num_slots = 8, unsigned num_caps = 32): Caps caps = my_memory.create_caps (3) caps.set (PARENT_MEMORY, parent_memory) caps.set (DATA, data) caps.set (PARENT, parent) iocall (caps.copy (), CAP_MASTER_DIRECT | RUN_STRING, Num (num_slots, num_caps)) Caps ret = get_arg () my_memory.destroy (caps) free_cap (caps) return ret 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) caps.set (PARENT_MEMORY, parent_memory) caps.set (DATA, data) caps.set (PARENT, parent) 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) return ret // Interface for talking to the parent process. struct Parent : public Cap: Parent (Cap c = Cap ()) : Cap (c): enum request: GET_CAPABILITY = Elfrun::ID PROVIDE_CAPABILITY WAIT GET_MEMORY PROVIDE_MEMORY INIT_DONE EXIT ID // Get a handle. template _T get_capability (unsigned num = 0): icall (Num (CAP_MASTER_DIRECT | GET_CAPABILITY, num), _T::ID) return get_arg () // Provide a device handle. template void provide_capability (Cap cap, unsigned num = 0): ocall (cap, Num (CAP_MASTER_DIRECT | PROVIDE_CAPABILITY, num), _T::ID) // Wait until a device is used by the caller again. template void wait (unsigned num = 0): call (Num (CAP_MASTER_DIRECT | WAIT, num), _T::ID) // 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. Memory get_memory (Cap target): iocall (target, CAP_MASTER_DIRECT | GET_MEMORY) return get_arg () // 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). Cap provide_memory (unsigned limit): icall (CAP_MASTER_DIRECT | PROVIDE_MEMORY, limit) return get_arg () // Signal the parent that the initialisation phase is over. void init_done (Num stage = 0): call (CAP_MASTER_DIRECT | INIT_DONE, stage) // Exit the program. The parent does not reply, but kills the process. void exit (Num code): call (CAP_MASTER_DIRECT | EXIT, code) // Keyboard interface. struct Keyboard : public Event: Keyboard (Cap c = Cap ()) : Event (c): enum request: GET_NUM_KEYS = Parent::ID 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) // Buzzer interface. struct Buzzer : public Device: Buzzer (Cap c = Cap ()) : Device (c): 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. void beep (unsigned freq, unsigned ms, unsigned volume, Cap cb = Cap ()): ocall (cb, Num (CAP_MASTER_DIRECT | BEEP, volume), Num (freq, ms)) // Abort current beep, if any. void stop (): call (CAP_MASTER_DIRECT | STOP) // Display interface. struct Display : public Device: Display (Cap c = Cap ()) : Device (c): enum request: SET_EOF_CB = Buzzer::ID MAP_FB UNMAP_FB 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. void set_eof_cb (Cap cb): ocall (cb, CAP_MASTER_DIRECT | SET_EOF_CB) // Map the framebuffer into memory. 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): ocall (caps, CAP_MASTER_DIRECT | UNMAP_FB) // Get information about the display. void get_info (): // TODO: Interface is to be designed. panic (0, "using undefined interface Display::get_info ()") // Numerical setting, such as a display backlight. struct Setting : public Device: Setting (Cap c = Cap ()) : Device (c): 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 // 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. // Directory interface. struct Directory : public Cap: Directory (Cap c = Cap ()) : Cap (c): enum request: GET_SIZE = Setting::ID GET_NAME GET_FILE_RO GET_FILE_INFO LOCK_RO UNLOCK_RO ID // Get the number of entries in this directory. Num get_size (): return call (CAP_MASTER_DIRECT | GET_SIZE) // Get the filename. String get_name (Num idx): icall (CAP_MASTER_DIRECT | GET_NAME, idx) return get_arg () // Get the file. Cap get_file_ro (Num idx): icall (CAP_MASTER_DIRECT | GET_FILE_RO, idx) return get_arg () // Get file info. TODO: define possible types. Num get_file_info (Num idx, unsigned type): return icall (Num (CAP_MASTER_DIRECT | GET_FILE_INFO, type), idx) // Lock the directory for reading. Multiple read locks can exist simultaneously, but no write lock can be present. void lock_ro (): call (CAP_MASTER_DIRECT | LOCK_RO) // Release a read-only lock. Write operations can only be done when the directory is locked. void unlock_ro (): call (CAP_MASTER_DIRECT | UNLOCK_RO) struct WDirectory : public Directory: WDirectory (Cap c = Cap ()) : Directory (c): enum request: GET_FILE = Directory::ID CREATE_FILE DELETE_FILE LOCK UNLOCK ID // Get the file. Cap get_file (Num idx): icall (CAP_MASTER_DIRECT | GET_FILE, idx) return get_arg () // Create a new file. After this, any index may map to a different file. Cap create_file (String name): icall (CAP_MASTER_DIRECT | CREATE_FILE) return get_arg () // Delete a file. After this, any index may map to a different file. void delete_file (Num idx): call (CAP_MASTER_DIRECT | DELETE_FILE, idx) // Lock the directory. Write operations can only be done when the directory is locked. void lock (): call (CAP_MASTER_DIRECT | LOCK) // Unlock the directory. Write operations can only be done when the directory is locked. void unlock (): call (CAP_MASTER_DIRECT | UNLOCK) // A filesystem turns a String into a Directory. struct Filesystem : public Device: Filesystem (Cap c = Cap ()) : Device (c): enum request: USE_DEVICE = WDirectory::ID USE_DEVICE_RO ID WDirectory use_device (WString dev): ocall (dev, CAP_MASTER_DIRECT | USE_DEVICE) return get_arg () Directory use_device_ro (String dev): ocall (dev, CAP_MASTER_DIRECT | USE_DEVICE_RO) return get_arg () // Stream interface. struct Stream : public Cap: Stream (Cap c = Cap ()) : Cap (c): enum request: READ = Filesystem::ID WRITE ID // Try to read size bytes. Returns the number of bytes successfully read. Num read (Num size, bool block): return icall (Num (CAP_MASTER_DIRECT | READ, block), size) // Try to write size bytes. Returns the number of bytes successfully written. Num write (String s, Num size): return ocall (s, CAP_MASTER_DIRECT | WRITE, size) struct UI : public Cap: UI (Cap c = Cap ()) : Cap (c): enum request: GET_STATE = Stream::ID EVENT EXIT ID enum constant: INPUT = 1 << 31 void get_state (Cap cap): ocall (cap, CAP_MASTER_DIRECT | GET_STATE) void event (unsigned code, unsigned value = 0): call (Num (CAP_MASTER_DIRECT | EVENT, code), value) void exit (): call (CAP_MASTER_DIRECT | EXIT) // Block device interface. struct Block_device : public WString: Block_device (Cap c = Cap ()) : WString (c): // TODO: to be designed. // TODO. // Sound interface. // Usb interfaces (port, device). // Pointer interface. (Only movement; buttons follow keyboard interface.) // Network interface. // Camera interface. #endif