2009-07-21 13:17:52 +03:00
|
|
|
#pypp 0
|
|
|
|
// Iris: micro-kernel for a capability-based operating system.
|
2010-01-14 19:14:37 +02:00
|
|
|
// bootstrap/init.ccp: Bootstrapping code.
|
2009-07-21 13:17:52 +03:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
#include "devices.hh"
|
2009-08-18 00:11:15 +03:00
|
|
|
#include "iris.hh"
|
2010-01-30 10:21:56 +02:00
|
|
|
#include "keys.hh"
|
2010-01-14 19:14:37 +02:00
|
|
|
#include <elf.h>
|
2009-07-21 13:17:52 +03:00
|
|
|
|
2010-01-24 22:34:24 +02:00
|
|
|
#define NUM_SLOTS 8
|
2010-01-16 17:13:54 +02:00
|
|
|
#define NUM_CAPS 32
|
2009-07-21 13:17:52 +03:00
|
|
|
|
2010-01-24 22:34:24 +02:00
|
|
|
enum Cap_codes:
|
|
|
|
SYSREQ = 1 << 16
|
|
|
|
THREAD
|
2010-01-30 10:21:56 +02:00
|
|
|
KEYBOARD
|
2010-01-24 22:34:24 +02:00
|
|
|
|
2010-01-14 19:14:37 +02:00
|
|
|
static unsigned _free
|
|
|
|
extern unsigned _end
|
2009-10-10 02:31:10 +03:00
|
|
|
|
2010-01-14 19:14:37 +02:00
|
|
|
void init_alloc ():
|
2010-01-17 11:01:42 +02:00
|
|
|
_free = ((unsigned)&_end + PAGE_SIZE - 1) & PAGE_MASK
|
2009-07-21 13:17:52 +03:00
|
|
|
|
2010-01-14 19:14:37 +02:00
|
|
|
char *alloc_space (unsigned pages):
|
|
|
|
unsigned ret = (_free + PAGE_SIZE - 1) & PAGE_MASK
|
2010-01-17 11:01:42 +02:00
|
|
|
_free = ret + (pages << PAGE_BITS)
|
2010-01-14 19:14:37 +02:00
|
|
|
return (char *)ret
|
|
|
|
|
|
|
|
void *operator new[] (unsigned size):
|
2010-01-17 11:01:42 +02:00
|
|
|
//kdebug ("new ")
|
2010-01-14 19:14:37 +02:00
|
|
|
void *ret = (void *)_free
|
|
|
|
size = (size + 3) & ~3
|
2010-01-16 17:13:54 +02:00
|
|
|
unsigned rest = PAGE_SIZE - (((_free - 1) & ~PAGE_MASK) + 1)
|
|
|
|
if rest < size:
|
|
|
|
unsigned pages = ((size - rest) + PAGE_SIZE - 1) >> PAGE_BITS
|
|
|
|
for unsigned p = 0; p < pages; ++p:
|
|
|
|
Kernel::Page page = Kernel::my_memory.create_page ()
|
|
|
|
page.set_flags (Kernel::Page::PAYING | Kernel::Page::FRAME, Kernel::Page::PAYING | Kernel::Page::FRAME)
|
2010-01-17 11:01:42 +02:00
|
|
|
Kernel::my_memory.map (page, _free + rest + (p << PAGE_BITS))
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::free_cap (page)
|
2010-01-14 19:14:37 +02:00
|
|
|
_free += size
|
2010-01-16 17:13:54 +02:00
|
|
|
//kdebug_num ((unsigned)ret)
|
|
|
|
//kdebug ("+")
|
|
|
|
//kdebug_num (size)
|
|
|
|
//kdebug ("\n")
|
2010-01-14 19:14:37 +02:00
|
|
|
return ret
|
|
|
|
|
2010-01-18 07:45:52 +02:00
|
|
|
void *operator new (unsigned size):
|
|
|
|
return new char[size]
|
|
|
|
|
2010-01-14 19:14:37 +02:00
|
|
|
struct file:
|
|
|
|
unsigned size
|
|
|
|
// Only the first 16 characters of the name are used, because that's much easier.
|
|
|
|
// This means that file names must be different in the first 16 characters if sort order matters.
|
|
|
|
char name[16]
|
|
|
|
String string
|
|
|
|
|
|
|
|
static unsigned num_files
|
|
|
|
static file *files
|
|
|
|
static unsigned *index
|
|
|
|
static Kernel::Memory top_memory
|
|
|
|
static unsigned slot
|
|
|
|
static unsigned max_pages
|
|
|
|
static char *mapping
|
|
|
|
static unsigned current_thread
|
2010-01-24 22:34:24 +02:00
|
|
|
static Kernel::Caps terminals
|
|
|
|
static unsigned terminal_slot
|
|
|
|
static Kernel::Caps memories
|
|
|
|
static unsigned memory_slot
|
2010-01-30 10:21:56 +02:00
|
|
|
static unsigned *inuse
|
2010-01-14 19:14:37 +02:00
|
|
|
|
|
|
|
// Get the initial block device and filesystem.
|
|
|
|
static Directory receive_devices ():
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Caps data, fs
|
2010-01-14 19:14:37 +02:00
|
|
|
bool have_data = false, have_fs = false
|
2010-01-16 17:13:54 +02:00
|
|
|
Device fs_dev, data_dev
|
2010-01-14 19:14:37 +02:00
|
|
|
for unsigned i = 0; i < 2; ++i:
|
2009-09-06 12:04:09 +03:00
|
|
|
Kernel::wait ()
|
2010-01-14 19:14:37 +02:00
|
|
|
if Kernel::recv.data[0].l != Parent::PROVIDE_DEVICE:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (Kernel::recv.data[0].l, "Invalid bootstrap request.")
|
2010-01-14 19:14:37 +02:00
|
|
|
switch Kernel::recv.data[1].l:
|
|
|
|
case String::ID:
|
|
|
|
if have_data:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (0, "duplicate device.")
|
2010-01-16 17:13:54 +02:00
|
|
|
data_dev = Kernel::get_arg ()
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::recv.reply.invoke ()
|
2010-01-16 17:13:54 +02:00
|
|
|
have_data = true
|
2009-10-10 02:31:10 +03:00
|
|
|
break
|
2010-01-14 19:14:37 +02:00
|
|
|
case Filesystem::ID:
|
|
|
|
if have_fs:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (0, "duplicate filesystem.")
|
2010-01-16 17:13:54 +02:00
|
|
|
fs_dev = Kernel::get_arg ()
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::recv.reply.invoke ()
|
2010-01-16 17:13:54 +02:00
|
|
|
have_fs = true
|
2009-07-21 13:17:52 +03:00
|
|
|
break
|
2009-09-07 00:34:50 +03:00
|
|
|
default:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (Kernel::recv.data[1].l, "unexpected device")
|
2010-01-14 19:14:37 +02:00
|
|
|
// Initialize the root file system.
|
2010-01-16 17:13:54 +02:00
|
|
|
data = data_dev.create_user (Kernel::my_memory)
|
|
|
|
data_dev.use (data)
|
|
|
|
fs = fs_dev.create_user (Kernel::my_memory)
|
|
|
|
fs_dev.use (fs)
|
2010-01-31 10:26:23 +02:00
|
|
|
Filesystem fs_cap = fs.get (0)
|
|
|
|
Directory root = fs_cap.use_device_ro (data.copy ())
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::free_cap (data)
|
|
|
|
Kernel::free_cap (fs)
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::free_cap (fs_cap)
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::free_cap (data_dev)
|
|
|
|
Kernel::free_cap (fs_dev)
|
2010-01-14 19:14:37 +02:00
|
|
|
return root
|
|
|
|
|
|
|
|
// Make a list of all files.
|
|
|
|
static void list_files (Directory root):
|
|
|
|
Kernel::Num fullsize = root.get_size ()
|
|
|
|
if fullsize.h != 0:
|
2010-01-16 17:13:54 +02:00
|
|
|
kdebug ("Too many files in bootstrap directory:")
|
|
|
|
kdebug_num (fullsize.h)
|
|
|
|
kdebug (":")
|
|
|
|
kdebug_num (fullsize.l)
|
|
|
|
kdebug ("\n")
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::panic (0)
|
|
|
|
num_files = fullsize.l
|
|
|
|
files = new file[num_files]
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::Caps caps = Kernel::my_memory.create_caps (num_files)
|
2010-01-14 19:14:37 +02:00
|
|
|
unsigned slot = Kernel::alloc_slot ()
|
|
|
|
caps.use (slot)
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::free_cap (caps)
|
2010-01-14 19:14:37 +02:00
|
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
|
|
String n = root.get_name (i)
|
|
|
|
n.get_chars (0, files[i].name)
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::free_cap (n)
|
|
|
|
Kernel::set_recv_arg (Kernel::Cap (slot, i))
|
2010-01-14 19:14:37 +02:00
|
|
|
files[i].string = root.get_file_ro (i)
|
|
|
|
Kernel::Num fullsize = files[i].string.get_size ()
|
|
|
|
if fullsize.h != 0:
|
2010-01-16 17:13:54 +02:00
|
|
|
kdebug ("initial file size too large: ")
|
|
|
|
kdebug_num (fullsize.h)
|
|
|
|
kdebug (":")
|
|
|
|
kdebug_num (fullsize.l)
|
|
|
|
kdebug (".\n")
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::panic (0)
|
|
|
|
files[i].size = fullsize.l
|
2010-01-16 17:13:54 +02:00
|
|
|
if max_pages < (fullsize.l + PAGE_SIZE - 1) >> PAGE_BITS:
|
|
|
|
max_pages = (fullsize.l + PAGE_SIZE - 1) >> PAGE_BITS
|
2010-01-14 19:14:37 +02:00
|
|
|
|
|
|
|
// Sort the list of files.
|
|
|
|
static bool is_less (file *f1, file *f2):
|
|
|
|
for unsigned i = 0; i < 16; ++i:
|
|
|
|
if f1->name[i] < f2->name[i]:
|
|
|
|
return true
|
|
|
|
if f1->name[i] > f2->name[i]:
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
|
|
|
|
// Bubble sort.
|
|
|
|
static void sort ():
|
|
|
|
index = new unsigned[num_files]
|
|
|
|
index[0] = 0
|
|
|
|
// Invariant: index[0...f-1] is sorted.
|
|
|
|
for unsigned f = 1; f < num_files; ++f:
|
|
|
|
// Bubble up until top. Test for less-than, because it wraps to maxunsigned.
|
|
|
|
unsigned i
|
|
|
|
// Invariant: index[0...f] \ index[i+1] is sorted and index[i+1...f] is sorted.
|
|
|
|
for i = f - 1; i < f; --i:
|
|
|
|
if is_less (&files[index[i]], &files[f]):
|
|
|
|
break
|
|
|
|
index[i + 1] = index[i]
|
|
|
|
index[i + 1] = f
|
|
|
|
|
2010-01-24 22:34:24 +02:00
|
|
|
static void run (file *f, bool priv, int id):
|
|
|
|
Kernel::Memory mem = Kernel::Cap (memory_slot, id)
|
|
|
|
Kernel::set_recv_arg (mem)
|
|
|
|
top_memory.create_memory ()
|
2010-01-16 17:13:54 +02:00
|
|
|
unsigned num_pages = (f->size + PAGE_SIZE - 1) >> PAGE_BITS
|
2010-01-14 19:14:37 +02:00
|
|
|
for unsigned p = 0; p < num_pages; ++p:
|
2010-01-17 11:01:42 +02:00
|
|
|
//kdebug_num (p)
|
|
|
|
//kdebug ("/")
|
|
|
|
//kdebug_num (num_pages)
|
|
|
|
//kdebug ("\n")
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::set_recv_arg (Kernel::Cap (slot, p))
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::my_memory.create_page ()
|
|
|
|
Kernel::Page (slot, p).set_flags (Kernel::Page::PAYING, Kernel::Page::PAYING)
|
2010-01-14 19:14:37 +02:00
|
|
|
f->string.get_page (p << PAGE_BITS, Kernel::Cap (slot, p))
|
|
|
|
Kernel::my_memory.map (Kernel::Cap (slot, p), (unsigned)&mapping[p << PAGE_BITS])
|
|
|
|
Kernel::Thread thread = mem.create_thread (NUM_SLOTS)
|
|
|
|
if priv:
|
|
|
|
thread.make_priv ()
|
|
|
|
Elf32_Ehdr *header = (Elf32_Ehdr *)mapping
|
|
|
|
for unsigned j = 0; j < SELFMAG; ++j:
|
|
|
|
if header->e_ident[j] != ELFMAG[j]:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (header->e_ident[j], "invalid ELF magic")
|
2010-01-14 19:14:37 +02:00
|
|
|
return
|
|
|
|
if header->e_ident[EI_CLASS] != ELFCLASS32:
|
2010-01-16 17:13:54 +02:00
|
|
|
kdebug ("invalid ELF class:")
|
|
|
|
kdebug_num (header->e_ident[EI_CLASS])
|
|
|
|
kdebug (" != ")
|
|
|
|
kdebug_num (ELFCLASS32)
|
|
|
|
kdebug ("\n")
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::panic (0)
|
|
|
|
return
|
|
|
|
if header->e_ident[EI_DATA] != ELFDATA2LSB:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (header->e_ident[EI_DATA], "invalid ELF data")
|
2010-01-14 19:14:37 +02:00
|
|
|
if header->e_ident[EI_VERSION] != EV_CURRENT:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (header->e_ident[EI_VERSION], "invalid ELF version")
|
2010-01-14 19:14:37 +02:00
|
|
|
if header->e_type != ET_EXEC:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (header->e_type, "invalid ELF type")
|
2010-01-14 19:14:37 +02:00
|
|
|
if header->e_machine != EM_MIPS_RS3_LE && header->e_machine != EM_MIPS:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (header->e_machine, "invalid ELF machine")
|
2010-01-14 19:14:37 +02:00
|
|
|
thread.set_pc (header->e_entry)
|
|
|
|
thread.set_sp (0x80000000)
|
|
|
|
for unsigned section = 0; section < header->e_shnum; ++section:
|
|
|
|
Elf32_Shdr *shdr = (Elf32_Shdr *)((unsigned)mapping + header->e_shoff + section * header->e_shentsize)
|
|
|
|
if ~shdr->sh_flags & SHF_ALLOC:
|
|
|
|
continue
|
|
|
|
bool readonly = !(shdr->sh_flags & SHF_WRITE)
|
|
|
|
//bool executable = shdr->sh_flags & SHF_EXEC_INSTR
|
|
|
|
if shdr->sh_type != SHT_NOBITS:
|
|
|
|
unsigned file_offset = shdr->sh_offset >> PAGE_BITS
|
|
|
|
if (file_offset + ((shdr->sh_size + PAGE_SIZE - 1) >> PAGE_BITS)) >= (PAGE_SIZE >> 2):
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (shdr->sh_size, "thread too large")
|
2010-01-14 19:14:37 +02:00
|
|
|
return
|
|
|
|
for unsigned p = (shdr->sh_addr & PAGE_MASK); p < shdr->sh_addr + shdr->sh_size; p += PAGE_SIZE:
|
|
|
|
unsigned section_offset = (p - (shdr->sh_addr & PAGE_MASK)) >> PAGE_BITS
|
|
|
|
unsigned idx = file_offset + section_offset
|
|
|
|
Kernel::Page page = mem.mapping ((void *)p)
|
|
|
|
if Kernel::recv.data[0].l == Kernel::NO_ERROR:
|
|
|
|
// The address already has a mapping; assume that it is correct.
|
|
|
|
Kernel::free_cap (page)
|
|
|
|
continue
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::free_cap (page)
|
2010-01-14 19:14:37 +02:00
|
|
|
page = mem.create_page ()
|
2010-01-17 11:01:42 +02:00
|
|
|
unsigned f
|
|
|
|
if readonly:
|
|
|
|
f = Kernel::Page::PAYING | Kernel::Page::MAPPED_READONLY
|
|
|
|
else:
|
|
|
|
f = Kernel::Page::PAYING
|
|
|
|
page.set_flags (f, f)
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::Page (slot, idx).share (page, 0)
|
2010-01-17 11:01:42 +02:00
|
|
|
//kdebug ("mapping at ")
|
|
|
|
//kdebug_num (p)
|
|
|
|
//if readonly:
|
|
|
|
// kdebug (" (readonly)")
|
|
|
|
//kdebug ("\n")
|
|
|
|
if !mem.map (page, p):
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (0, "unable to map page")
|
2010-01-14 19:14:37 +02:00
|
|
|
return
|
|
|
|
Kernel::free_cap (page)
|
|
|
|
else:
|
|
|
|
if readonly:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (0, "unwritable bss section")
|
2010-01-14 19:14:37 +02:00
|
|
|
return
|
|
|
|
for unsigned p = (shdr->sh_addr & PAGE_MASK); p < shdr->sh_addr + shdr->sh_size; p += PAGE_SIZE:
|
|
|
|
Kernel::Page page = mem.mapping ((void *)p)
|
|
|
|
if Kernel::recv.data[0].l == Kernel::NO_ERROR:
|
2010-01-16 17:13:54 +02:00
|
|
|
// No error means there is a mapping.
|
|
|
|
Kernel::free_cap (page)
|
|
|
|
for unsigned a = p; a < ((p + PAGE_SIZE) & PAGE_MASK); a += 4:
|
|
|
|
if a >= shdr->sh_addr + shdr->sh_size:
|
|
|
|
break
|
|
|
|
if a < shdr->sh_addr:
|
|
|
|
continue
|
|
|
|
((unsigned *)&mapping[p - shdr->sh_addr])[(a & ~PAGE_MASK) >> 2] = 0
|
|
|
|
else:
|
|
|
|
Kernel::free_cap (page)
|
2010-01-14 19:14:37 +02:00
|
|
|
page = mem.create_page ()
|
|
|
|
if Kernel::recv.data[0].l != Kernel::NO_ERROR:
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (Kernel::recv.data[0].l, "out of memory")
|
2010-01-14 19:14:37 +02:00
|
|
|
if !page.set_flags (Kernel::Page::PAYING | Kernel::Page::FRAME, Kernel::Page::PAYING | Kernel::Page::FRAME):
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (0, "out of memory")
|
2010-01-14 19:14:37 +02:00
|
|
|
if !mem.map (page, p):
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (0, "unable to map bss page")
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::free_cap (page)
|
2010-01-17 11:01:42 +02:00
|
|
|
for unsigned p = 0; p < num_pages; ++p:
|
2010-01-16 17:13:54 +02:00
|
|
|
Kernel::my_memory.destroy (Kernel::Page (slot, p))
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::Page stackpage = mem.create_page ()
|
|
|
|
stackpage.set_flags (Kernel::Page::PAYING | Kernel::Page::FRAME, Kernel::Page::PAYING | Kernel::Page::FRAME)
|
|
|
|
if Kernel::recv.data[0].l != Kernel::NO_ERROR || !mem.map (stackpage, 0x7ffff000):
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::panic (Kernel::recv.data[0].l, "unable to map initial stack page")
|
2010-01-14 19:14:37 +02:00
|
|
|
Kernel::free_cap (stackpage)
|
|
|
|
Kernel::Caps caps = mem.create_caps (NUM_CAPS)
|
|
|
|
thread.use (caps, 0)
|
|
|
|
thread.set_info (Kernel::Thread::A0, NUM_SLOTS)
|
|
|
|
thread.set_info (Kernel::Thread::A1, NUM_CAPS)
|
|
|
|
Kernel::Receiver receiver = mem.create_receiver ()
|
|
|
|
receiver.set_owner (thread.copy ())
|
|
|
|
Kernel::Cap call = receiver.create_call_capability ()
|
2010-01-24 22:34:24 +02:00
|
|
|
Kernel::Cap parent = Kernel::my_receiver.create_capability (Kernel::Num (current_thread++, THREAD))
|
2010-01-14 19:14:37 +02:00
|
|
|
caps.set (__receiver_num, receiver.copy ())
|
|
|
|
caps.set (__thread_num, thread.copy ())
|
|
|
|
caps.set (__memory_num, mem.copy ())
|
|
|
|
caps.set (__call_num, call.copy ())
|
|
|
|
caps.set (__parent_num, parent.copy ())
|
|
|
|
thread.run ()
|
|
|
|
Kernel::free_cap (receiver)
|
|
|
|
Kernel::free_cap (thread)
|
|
|
|
Kernel::free_cap (call)
|
|
|
|
Kernel::free_cap (parent)
|
2010-01-17 11:01:42 +02:00
|
|
|
Kernel::free_cap (caps)
|
2009-08-05 11:16:24 +03:00
|
|
|
|
2010-01-18 06:01:59 +02:00
|
|
|
static void kdebug_name (char const *t, file *f):
|
|
|
|
kdebug (t)
|
|
|
|
for unsigned j = 0; j < 16; ++j:
|
|
|
|
if f->name[j] != 0:
|
|
|
|
kdebug_char (f->name[j])
|
|
|
|
kdebug ("...")
|
|
|
|
|
2010-01-24 22:34:24 +02:00
|
|
|
static Device sysreq_dev
|
|
|
|
|
2010-01-18 07:45:52 +02:00
|
|
|
struct Dev:
|
|
|
|
static Dev *devs
|
2010-01-24 22:34:24 +02:00
|
|
|
static unsigned num_devs
|
2010-01-18 07:45:52 +02:00
|
|
|
Dev *next
|
2010-01-24 22:34:24 +02:00
|
|
|
unsigned code, idx, id
|
2010-01-18 07:45:52 +02:00
|
|
|
Device dev
|
|
|
|
static Dev *find (unsigned c, unsigned i):
|
|
|
|
for Dev *d = devs; d; d = d->next:
|
|
|
|
if d->code == c && d->idx == i:
|
|
|
|
return d
|
|
|
|
return NULL
|
|
|
|
static void add (unsigned c, unsigned i, Kernel::Cap cap):
|
2010-01-24 22:34:24 +02:00
|
|
|
if c == Keyboard::ID && i == 1:
|
|
|
|
sysreq_dev = cap
|
|
|
|
return
|
2010-01-18 07:45:52 +02:00
|
|
|
while find (c, i):
|
|
|
|
++i
|
|
|
|
Dev *d = new Dev ()
|
|
|
|
d->next = devs
|
|
|
|
d->code = c
|
|
|
|
d->idx = i
|
|
|
|
d->dev = cap
|
|
|
|
devs = d
|
2010-01-24 22:34:24 +02:00
|
|
|
d->id = num_devs++
|
2010-01-18 07:45:52 +02:00
|
|
|
static void kdebug_list ():
|
|
|
|
for Dev *d = devs; d; d = d->next:
|
|
|
|
kdebug (">")
|
|
|
|
kdebug_num (d->code)
|
|
|
|
kdebug (":")
|
|
|
|
kdebug_num (d->idx)
|
|
|
|
|
|
|
|
Dev *Dev::devs
|
2010-01-24 22:34:24 +02:00
|
|
|
unsigned Dev::num_devs
|
2010-01-18 07:45:52 +02:00
|
|
|
|
2010-01-24 22:34:24 +02:00
|
|
|
static void handle_init (unsigned id):
|
2010-01-18 07:45:52 +02:00
|
|
|
while true:
|
|
|
|
Kernel::wait ()
|
|
|
|
switch Kernel::recv.data[0].l:
|
|
|
|
case Parent::PROVIDE_DEVICE:
|
2010-01-18 08:31:19 +02:00
|
|
|
kdebug ("adding dev ")
|
2010-01-18 07:45:52 +02:00
|
|
|
kdebug_num (Kernel::recv.data[1].l)
|
|
|
|
kdebug (":")
|
|
|
|
kdebug_num (Kernel::recv.data[0].h)
|
2010-01-24 22:34:24 +02:00
|
|
|
kdebug (" as ")
|
|
|
|
kdebug_num (Dev::num_devs)
|
2010-01-18 07:45:52 +02:00
|
|
|
kdebug ("\n")
|
2010-01-18 08:31:19 +02:00
|
|
|
Kernel::Cap reply = Kernel::get_reply ()
|
|
|
|
Dev::add (Kernel::recv.data[1].l, Kernel::recv.data[0].h, Kernel::get_arg ())
|
|
|
|
reply.invoke ()
|
|
|
|
Kernel::free_cap (reply)
|
2010-01-18 07:45:52 +02:00
|
|
|
break
|
|
|
|
case Parent::INIT_DONE:
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Cap reply = Kernel::get_reply ()
|
|
|
|
memories.set (id + num_files, reply.copy ())
|
|
|
|
Kernel::free_cap (reply)
|
2010-01-18 07:45:52 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
kdebug ("unknown init request\n")
|
2010-01-24 22:34:24 +02:00
|
|
|
Kernel::panic (Kernel::recv.data[0].l)
|
|
|
|
|
|
|
|
static void get_device ():
|
|
|
|
Kernel::Cap reply = Kernel::get_reply ()
|
|
|
|
unsigned id = Kernel::recv.protected_data.l
|
|
|
|
Dev *d = Dev::find (Kernel::recv.data[1].l, Kernel::recv.data[0].h)
|
|
|
|
if d:
|
2010-01-30 10:21:56 +02:00
|
|
|
if inuse[id * Dev::num_devs + d->id] > 0:
|
|
|
|
Kernel::panic (id, "requesting device twice")
|
2010-01-24 22:34:24 +02:00
|
|
|
kdebug ("giving dev ")
|
|
|
|
kdebug_num (Kernel::recv.data[1].l)
|
|
|
|
kdebug (":")
|
|
|
|
kdebug_num (Kernel::recv.data[0].h)
|
|
|
|
kdebug (" = ")
|
|
|
|
kdebug_num (d->id)
|
|
|
|
kdebug (" to ")
|
|
|
|
kdebug_num (id)
|
|
|
|
kdebug ("\n")
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Caps cap = d->dev.create_user (Kernel::Memory (memory_slot, id), 0, 0x15000)
|
2010-01-30 10:21:56 +02:00
|
|
|
Kernel::Caps (terminal_slot, id).set (d->id, cap.copy ())
|
|
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
|
|
if inuse[i * Dev::num_devs + d->id] == 2:
|
|
|
|
inuse[i * Dev::num_devs + d->id] = 1
|
|
|
|
inuse[id * Dev::num_devs + d->id] = 2
|
2010-01-24 22:34:24 +02:00
|
|
|
d->dev.use (cap)
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Cap ret = cap.get (0)
|
|
|
|
reply.invoke (0, 0, ret.copy ())
|
|
|
|
Kernel::free_cap (ret)
|
2010-01-24 22:34:24 +02:00
|
|
|
Kernel::free_cap (cap)
|
|
|
|
else:
|
|
|
|
kdebug ("device not found: ")
|
|
|
|
kdebug_num (Kernel::recv.data[1].l)
|
|
|
|
kdebug (":")
|
|
|
|
kdebug_num (Kernel::recv.data[0].h)
|
|
|
|
Dev::kdebug_list ()
|
|
|
|
kdebug ("\n")
|
|
|
|
reply.invoke (~0, ~0)
|
|
|
|
Kernel::panic (0)
|
|
|
|
Kernel::free_cap (reply)
|
2010-01-18 07:45:52 +02:00
|
|
|
|
2010-02-06 00:15:58 +02:00
|
|
|
static void draw_ball ():
|
|
|
|
int const r = 50
|
|
|
|
for int y = -r; y < r; ++y:
|
|
|
|
for int x = -r; x < r; ++x:
|
|
|
|
if x * x + y * y > r * r:
|
|
|
|
((unsigned *)0x15000)[(120 + y) * 320 + 160 + x] = 0x000000
|
|
|
|
else:
|
|
|
|
((unsigned *)0x15000)[(120 + y) * 320 + 160 + x] = 0x3f30ff
|
|
|
|
|
|
|
|
static void draw_square ():
|
|
|
|
int const r = 50
|
|
|
|
for int y = -r; y < r; ++y:
|
|
|
|
for int x = -r; x < r; ++x:
|
|
|
|
((unsigned *)0x15000)[(120 + y) * 320 + 160 + x] = 0xffff00
|
|
|
|
|
2009-09-06 12:04:09 +03:00
|
|
|
Kernel::Num start ():
|
2010-01-14 19:14:37 +02:00
|
|
|
// Wait for the debugging device to be active, in case there is one.
|
2009-12-26 15:17:06 +02:00
|
|
|
Kernel::schedule ()
|
2010-01-18 08:31:19 +02:00
|
|
|
Dev::devs = NULL
|
2010-01-24 22:34:24 +02:00
|
|
|
Dev::num_devs = 0
|
2010-01-16 17:13:54 +02:00
|
|
|
init_alloc ()
|
2010-01-14 19:14:37 +02:00
|
|
|
top_memory = Kernel::get_top_memory ()
|
|
|
|
Directory root = receive_devices ()
|
|
|
|
root.lock_ro ()
|
|
|
|
list_files (root)
|
|
|
|
sort ()
|
|
|
|
Kernel::Caps caps = Kernel::my_memory.create_caps (max_pages)
|
|
|
|
slot = caps.use ()
|
|
|
|
mapping = alloc_space (max_pages)
|
2010-01-24 22:34:24 +02:00
|
|
|
terminals = Kernel::my_memory.create_caps (num_files)
|
|
|
|
terminal_slot = terminals.use ()
|
|
|
|
// Two times, because it holds the memory and the init_done reply for each task.
|
|
|
|
memories = Kernel::my_memory.create_caps (num_files * 2)
|
|
|
|
memory_slot = memories.use ()
|
2010-01-14 19:14:37 +02:00
|
|
|
for unsigned i = 0; i < num_files; ++i:
|
2010-01-18 06:01:59 +02:00
|
|
|
kdebug_name ("loading ", &files[index[i]])
|
2010-01-24 22:34:24 +02:00
|
|
|
run (&files[index[i]], files[index[i]].name[0] == '#', i)
|
2010-01-18 06:01:59 +02:00
|
|
|
kdebug ("running\n")
|
2010-01-24 22:34:24 +02:00
|
|
|
handle_init (i)
|
2010-01-30 10:21:56 +02:00
|
|
|
inuse = new unsigned[num_files * Dev::num_devs]
|
|
|
|
for unsigned f = 0; f < num_files; ++f:
|
|
|
|
for unsigned d = 0; d < Dev::num_devs; ++d:
|
|
|
|
inuse[f * Dev::num_devs + d] = 0
|
2010-01-24 22:34:24 +02:00
|
|
|
// Notify all programs that they may start.
|
|
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
|
|
Kernel::Cap (memory_slot, i + num_files).invoke ()
|
|
|
|
// create terminals.
|
|
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
|
|
Kernel::Caps term (terminal_slot, i)
|
|
|
|
Kernel::Memory mem (memory_slot, i)
|
|
|
|
Kernel::set_recv_arg (term)
|
|
|
|
mem.create_caps (Dev::num_devs)
|
|
|
|
// set up system request.
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Caps sysreq_caps = sysreq_dev.create_user (Kernel::my_memory)
|
|
|
|
sysreq_dev.use (sysreq_caps)
|
|
|
|
Keyboard sysreq = sysreq_caps.get (0)
|
2010-01-24 22:34:24 +02:00
|
|
|
Kernel::free_cap (sysreq_dev)
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::free_cap (sysreq_caps)
|
2010-01-24 22:34:24 +02:00
|
|
|
Kernel::Cap cap = Kernel::my_receiver.create_capability (Kernel::Num (0, SYSREQ))
|
|
|
|
sysreq.set_cb (cap.copy ())
|
|
|
|
Kernel::free_cap (sysreq)
|
|
|
|
Kernel::free_cap (cap)
|
2010-01-30 10:21:56 +02:00
|
|
|
// set up own capabilities.
|
|
|
|
Dev *display_dev = Dev::find (Display::ID, 0)
|
|
|
|
if !display_dev:
|
|
|
|
Kernel::panic (0, "no display")
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Caps display_caps = display_dev->dev.create_user (Kernel::my_memory, 0, 0x15000)
|
|
|
|
Display display = display_caps.get (0)
|
2010-02-06 00:15:58 +02:00
|
|
|
draw_ball ()
|
2010-01-30 10:21:56 +02:00
|
|
|
Dev *keyboard_dev = Dev::find (Keyboard::ID, 0)
|
|
|
|
if !keyboard_dev:
|
|
|
|
Kernel::panic (0, "no keyboard")
|
2010-01-31 10:26:23 +02:00
|
|
|
Kernel::Caps keyboard_caps = keyboard_dev->dev.create_user (Kernel::my_memory, 0, 0x15000)
|
|
|
|
Keyboard keyboard = keyboard_caps.get (0)
|
2010-01-18 08:31:19 +02:00
|
|
|
kdebug ("init done\n")
|
2010-01-14 19:14:37 +02:00
|
|
|
root.unlock_ro ()
|
|
|
|
Kernel::free_slot (slot)
|
|
|
|
Kernel::my_memory.destroy (caps)
|
2010-01-30 10:21:56 +02:00
|
|
|
Dev **waiter = new Dev *[num_files]
|
|
|
|
Kernel::Cap keyboard_cb = Kernel::my_receiver.create_capability (Kernel::Num (0, KEYBOARD))
|
|
|
|
bool in_system = false
|
2010-01-17 11:01:42 +02:00
|
|
|
while true:
|
2010-01-24 22:34:24 +02:00
|
|
|
Kernel::wait ()
|
|
|
|
switch Kernel::recv.protected_data.h:
|
|
|
|
case SYSREQ:
|
|
|
|
// System request.
|
|
|
|
if Kernel::recv.data[0].l & Keyboard::RELEASE:
|
|
|
|
continue
|
2010-01-31 10:26:23 +02:00
|
|
|
keyboard_dev->dev.use (keyboard_caps)
|
|
|
|
display_dev->dev.use (display_caps)
|
2010-01-30 10:21:56 +02:00
|
|
|
keyboard.set_cb (keyboard_cb)
|
|
|
|
in_system = true
|
2010-01-24 22:34:24 +02:00
|
|
|
continue
|
|
|
|
case THREAD:
|
|
|
|
// Request for something from a child thread.
|
|
|
|
switch Kernel::recv.data[0].l:
|
|
|
|
case Parent::INIT_DONE:
|
|
|
|
Kernel::panic (Kernel::recv.protected_data.l, "double init_done")
|
|
|
|
case Parent::PROVIDE_DEVICE:
|
|
|
|
Kernel::panic (Kernel::recv.protected_data.l, "too late now for provide")
|
|
|
|
case Parent::GET_DEVICE:
|
|
|
|
get_device ()
|
|
|
|
break
|
2010-01-30 10:21:56 +02:00
|
|
|
case Parent::WAIT:
|
|
|
|
unsigned id = Kernel::recv.protected_data.l
|
|
|
|
Dev *dev = Dev::find (Kernel::recv.data[1].l, 0)
|
|
|
|
if id >= num_files:
|
|
|
|
Kernel::panic (0, "invalid id")
|
|
|
|
if !dev:
|
|
|
|
Kernel::panic (0, "invalid dev")
|
|
|
|
switch inuse[id * Dev::num_devs + dev->id]:
|
|
|
|
case 0:
|
|
|
|
Kernel::panic (0, "waiting for non-owned device")
|
|
|
|
case 2:
|
|
|
|
if !in_system:
|
|
|
|
Kernel::recv.reply.invoke ()
|
|
|
|
break
|
|
|
|
// fall through.
|
|
|
|
case 1:
|
|
|
|
Kernel::Cap reply = Kernel::get_reply ()
|
|
|
|
memories.set (id + num_files, reply.copy ())
|
|
|
|
Kernel::free_cap (reply)
|
|
|
|
waiter[id] = dev
|
|
|
|
break
|
|
|
|
break
|
2010-01-24 22:34:24 +02:00
|
|
|
case Parent::EXIT:
|
|
|
|
Kernel::panic (Kernel::recv.protected_data.l, "exit is not supported")
|
|
|
|
default:
|
|
|
|
Kernel::panic (Kernel::recv.data[0].l, "invalid operation from child")
|
|
|
|
break
|
2010-01-30 10:21:56 +02:00
|
|
|
case KEYBOARD:
|
2010-02-06 00:15:58 +02:00
|
|
|
if !in_system:
|
|
|
|
break
|
|
|
|
if Kernel::recv.data[0].l == (Key::ENTER | Keyboard::RELEASE):
|
2010-01-30 10:21:56 +02:00
|
|
|
for Dev *d = Dev::devs; d; d = d->next:
|
|
|
|
if d->idx != 0:
|
|
|
|
continue
|
|
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
|
|
if inuse[i * Dev::num_devs + d->id] == 2:
|
|
|
|
Kernel::Cap c = Kernel::Caps (terminal_slot, i).get (d->id)
|
|
|
|
d->dev.use (c)
|
|
|
|
Kernel::free_cap (c)
|
|
|
|
if waiter[i] == d:
|
|
|
|
Kernel::Cap (memory_slot, i + num_files).invoke ()
|
|
|
|
waiter[i] = NULL
|
|
|
|
in_system = false
|
|
|
|
break
|
|
|
|
if Kernel::recv.data[0].l & Keyboard::RELEASE:
|
|
|
|
continue
|
2010-01-31 10:26:23 +02:00
|
|
|
unsigned which = 0
|
2010-01-30 10:21:56 +02:00
|
|
|
switch Kernel::recv.data[0].l:
|
|
|
|
case Key::VOLUME_DOWN:
|
2010-01-31 10:26:23 +02:00
|
|
|
// Set ball.
|
2010-02-06 00:15:58 +02:00
|
|
|
draw_ball ()
|
2010-01-31 10:26:23 +02:00
|
|
|
which = 2
|
2010-01-30 10:21:56 +02:00
|
|
|
break
|
2010-01-31 10:26:23 +02:00
|
|
|
case Key::VOLUME_UP:
|
|
|
|
// Set square.
|
2010-02-06 00:15:58 +02:00
|
|
|
draw_square ()
|
2010-01-31 10:26:23 +02:00
|
|
|
which = 1
|
|
|
|
break
|
|
|
|
if which != 0:
|
|
|
|
for Dev *d = Dev::devs; d; d = d->next:
|
|
|
|
if d->code != Display::ID || d->idx != 0:
|
|
|
|
continue
|
|
|
|
for unsigned f = 0; f < num_files; ++f:
|
|
|
|
if inuse[f * Dev::num_devs + d->id] == 0:
|
|
|
|
continue
|
|
|
|
inuse[f * Dev::num_devs + d->id] = (--which ? 2 : 1)
|
2010-01-30 10:21:56 +02:00
|
|
|
break
|
2010-01-24 22:34:24 +02:00
|
|
|
default:
|
|
|
|
Kernel::panic (Kernel::recv.protected_data.h, "unknown source of request")
|