mirror of
git://projects.qi-hardware.com/iris.git
synced 2024-11-17 21:30:37 +02:00
405 lines
12 KiB
COBOL
405 lines
12 KiB
COBOL
#pypp 0
|
|
// Iris: micro-kernel for a capability-based operating system.
|
|
// bootstrap/init.ccp: Bootstrapping code.
|
|
// 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"
|
|
#include "iris.hh"
|
|
#include <elf.h>
|
|
|
|
#define NUM_SLOTS 4
|
|
#define NUM_CAPS 32
|
|
|
|
static unsigned _free
|
|
extern unsigned _end
|
|
|
|
void init_alloc ():
|
|
_free = (unsigned)&_end
|
|
|
|
char *alloc_space (unsigned pages):
|
|
unsigned ret = (_free + PAGE_SIZE - 1) & PAGE_MASK
|
|
_free = ret + pages * PAGE_SIZE
|
|
return (char *)ret
|
|
|
|
void *operator new[] (unsigned size):
|
|
void *ret = (void *)_free
|
|
size = (size + 3) & ~3
|
|
unsigned rest = PAGE_SIZE - (((_free - 1) & ~PAGE_MASK) + 1)
|
|
if rest < size:
|
|
unsigned pages = ((size - rest) + PAGE_SIZE - 1) >> PAGE_BITS
|
|
char *space = alloc_space (pages)
|
|
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)
|
|
Kernel::my_memory.map (page, (unsigned)&space[p << PAGE_BITS])
|
|
Kernel::free_cap (page)
|
|
_free += size
|
|
//kdebug_num ((unsigned)ret)
|
|
//kdebug ("+")
|
|
//kdebug_num (size)
|
|
//kdebug ("\n")
|
|
return ret
|
|
|
|
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
|
|
|
|
// Get the initial block device and filesystem.
|
|
static Directory receive_devices ():
|
|
String data
|
|
Filesystem fs
|
|
bool have_data = false, have_fs = false
|
|
Device fs_dev, data_dev
|
|
for unsigned i = 0; i < 2; ++i:
|
|
Kernel::wait ()
|
|
if Kernel::recv.data[0].l != Parent::PROVIDE_DEVICE:
|
|
kdebug ("Invalid bootstrap request.\n")
|
|
Kernel::panic (0)
|
|
switch Kernel::recv.data[1].l:
|
|
case String::ID:
|
|
if have_data:
|
|
kdebug ("duplicate device.\n")
|
|
Kernel::panic (0)
|
|
data_dev = Kernel::get_arg ()
|
|
Kernel::recv.reply.invoke ()
|
|
have_data = true
|
|
break
|
|
case Filesystem::ID:
|
|
if have_fs:
|
|
kdebug ("duplicate filesystem.\n")
|
|
Kernel::panic (0)
|
|
fs_dev = Kernel::get_arg ()
|
|
Kernel::recv.reply.invoke ()
|
|
have_fs = true
|
|
break
|
|
default:
|
|
kdebug ("unexpected device: ")
|
|
kdebug_num (Kernel::recv.data[1].l)
|
|
kdebug_char ('\n')
|
|
Kernel::panic (0)
|
|
// Initialize the root file system.
|
|
data = data_dev.create_user (Kernel::my_memory)
|
|
data_dev.use (data)
|
|
fs = fs_dev.create_user (Kernel::my_memory)
|
|
fs_dev.use (fs)
|
|
Directory root = fs.use_device_ro (data.copy ())
|
|
Kernel::free_cap (data)
|
|
Kernel::free_cap (fs)
|
|
Kernel::free_cap (data_dev)
|
|
Kernel::free_cap (fs_dev)
|
|
return root
|
|
|
|
// Make a list of all files.
|
|
static void list_files (Directory root):
|
|
Kernel::Num fullsize = root.get_size ()
|
|
if fullsize.h != 0:
|
|
kdebug ("Too many files in bootstrap directory:")
|
|
kdebug_num (fullsize.h)
|
|
kdebug (":")
|
|
kdebug_num (fullsize.l)
|
|
kdebug ("\n")
|
|
Kernel::panic (0)
|
|
num_files = fullsize.l
|
|
kdebug ("files in directory: ")
|
|
kdebug_num (num_files)
|
|
kdebug ("\n")
|
|
files = new file[num_files]
|
|
Kernel::Caps caps = Kernel::my_memory.create_caps (num_files)
|
|
unsigned slot = Kernel::alloc_slot ()
|
|
caps.use (slot)
|
|
Kernel::free_cap (caps)
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
String n = root.get_name (i)
|
|
n.get_chars (0, files[i].name)
|
|
kdebug ("file: ")
|
|
for unsigned j = 0; j < 16; ++j:
|
|
if files[i].name[j] != 0:
|
|
kdebug_char (files[i].name[j])
|
|
kdebug_char ('\n')
|
|
Kernel::free_cap (n)
|
|
Kernel::set_recv_arg (Kernel::Cap (slot, i))
|
|
files[i].string = root.get_file_ro (i)
|
|
Kernel::Num fullsize = files[i].string.get_size ()
|
|
if fullsize.h != 0:
|
|
kdebug ("initial file size too large: ")
|
|
kdebug_num (fullsize.h)
|
|
kdebug (":")
|
|
kdebug_num (fullsize.l)
|
|
kdebug (".\n")
|
|
Kernel::panic (0)
|
|
files[i].size = fullsize.l
|
|
if max_pages < (fullsize.l + PAGE_SIZE - 1) >> PAGE_BITS:
|
|
max_pages = (fullsize.l + PAGE_SIZE - 1) >> PAGE_BITS
|
|
|
|
// 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
|
|
|
|
static void run (file *f, bool priv):
|
|
Kernel::Memory mem = top_memory.create_memory ()
|
|
unsigned num_pages = (f->size + PAGE_SIZE - 1) >> PAGE_BITS
|
|
for unsigned p = 0; p < num_pages; ++p:
|
|
kdebug_num (p)
|
|
kdebug ("/")
|
|
kdebug_num (num_pages)
|
|
kdebug ("\n")
|
|
Kernel::set_recv_arg (Kernel::Cap (slot, p))
|
|
Kernel::my_memory.create_page ()
|
|
Kernel::Page (slot, p).set_flags (Kernel::Page::PAYING, Kernel::Page::PAYING)
|
|
f->string.get_page (p << PAGE_BITS, Kernel::Cap (slot, p))
|
|
kdebug_line ()
|
|
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]:
|
|
kdebug ("invalid ELF magic\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if header->e_ident[EI_CLASS] != ELFCLASS32:
|
|
kdebug ("invalid ELF class:")
|
|
kdebug_num (header->e_ident[EI_CLASS])
|
|
kdebug (" != ")
|
|
kdebug_num (ELFCLASS32)
|
|
kdebug ("\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if header->e_ident[EI_DATA] != ELFDATA2LSB:
|
|
kdebug ("invalid ELF data\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if header->e_ident[EI_VERSION] != EV_CURRENT:
|
|
kdebug ("invalid ELF version\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if header->e_type != ET_EXEC:
|
|
kdebug ("invalid ELF type\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if header->e_machine != EM_MIPS_RS3_LE && header->e_machine != EM_MIPS:
|
|
kdebug ("invalid ELF machine\n")
|
|
Kernel::panic (0)
|
|
return
|
|
thread.set_pc (header->e_entry)
|
|
thread.set_sp (0x80000000)
|
|
kdebug_line ()
|
|
for unsigned section = 0; section < header->e_shnum; ++section:
|
|
kdebug_line ()
|
|
Elf32_Shdr *shdr = (Elf32_Shdr *)((unsigned)mapping + header->e_shoff + section * header->e_shentsize)
|
|
if ~shdr->sh_flags & SHF_ALLOC:
|
|
continue
|
|
kdebug_line ()
|
|
bool readonly = !(shdr->sh_flags & SHF_WRITE)
|
|
//bool executable = shdr->sh_flags & SHF_EXEC_INSTR
|
|
kdebug_line ()
|
|
if shdr->sh_type != SHT_NOBITS:
|
|
kdebug_line ()
|
|
unsigned file_offset = shdr->sh_offset >> PAGE_BITS
|
|
if (file_offset + ((shdr->sh_size + PAGE_SIZE - 1) >> PAGE_BITS)) >= (PAGE_SIZE >> 2):
|
|
kdebug ("thread too large\n")
|
|
Kernel::panic (0)
|
|
return
|
|
kdebug_line ()
|
|
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
|
|
Kernel::free_cap (page)
|
|
page = mem.create_page ()
|
|
page.set_flags (Kernel::Page::PAYING, Kernel::Page::PAYING)
|
|
Kernel::Page (slot, idx).share (page, 0)
|
|
kdebug ("mapping at ")
|
|
kdebug_num (p)
|
|
kdebug ("\n")
|
|
if !mem.map (page, p, readonly):
|
|
kdebug ("unable to map page\n")
|
|
Kernel::panic (0)
|
|
return
|
|
Kernel::free_cap (page)
|
|
kdebug_line ()
|
|
else:
|
|
kdebug_line ()
|
|
if readonly:
|
|
kdebug ("unwritable bss section\n")
|
|
Kernel::panic (0)
|
|
return
|
|
kdebug_line ()
|
|
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:
|
|
// 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)
|
|
page = mem.create_page ()
|
|
if Kernel::recv.data[0].l != Kernel::NO_ERROR:
|
|
kdebug ("out of memory\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if !page.set_flags (Kernel::Page::PAYING | Kernel::Page::FRAME, Kernel::Page::PAYING | Kernel::Page::FRAME):
|
|
kdebug ("out of memory\n")
|
|
Kernel::panic (0)
|
|
return
|
|
if !mem.map (page, p):
|
|
kdebug ("unable to map bss page\n")
|
|
Kernel::panic (0)
|
|
return
|
|
Kernel::free_cap (page)
|
|
kdebug_line ()
|
|
kdebug_line ()
|
|
for unsigned p = 0; p <= num_pages; ++p:
|
|
Kernel::my_memory.destroy (Kernel::Page (slot, p))
|
|
kdebug_line ()
|
|
Kernel::Page stackpage = mem.create_page ()
|
|
kdebug_line ()
|
|
stackpage.set_flags (Kernel::Page::PAYING | Kernel::Page::FRAME, Kernel::Page::PAYING | Kernel::Page::FRAME)
|
|
kdebug_line ()
|
|
if Kernel::recv.data[0].l != Kernel::NO_ERROR || !mem.map (stackpage, 0x7ffff000):
|
|
kdebug ("unable to map initial stack page\n")
|
|
Kernel::panic (0)
|
|
return
|
|
kdebug_line ()
|
|
Kernel::free_cap (stackpage)
|
|
Kernel::Caps caps = mem.create_caps (NUM_CAPS)
|
|
kdebug_line ()
|
|
thread.use (caps, 0)
|
|
thread.set_info (Kernel::Thread::A0, NUM_SLOTS)
|
|
thread.set_info (Kernel::Thread::A1, NUM_CAPS)
|
|
kdebug_line ()
|
|
Kernel::Receiver receiver = mem.create_receiver ()
|
|
receiver.set_owner (thread.copy ())
|
|
Kernel::Cap call = receiver.create_call_capability ()
|
|
Kernel::Cap parent = Kernel::my_receiver.create_capability (++current_thread)
|
|
kdebug_line ()
|
|
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 ())
|
|
kdebug_line ()
|
|
thread.run ()
|
|
kdebug_line ()
|
|
Kernel::free_cap (receiver)
|
|
Kernel::free_cap (thread)
|
|
Kernel::free_cap (mem)
|
|
Kernel::free_cap (call)
|
|
Kernel::free_cap (parent)
|
|
kdebug_line ()
|
|
Kernel::print_caps ()
|
|
|
|
static void dump_devices ():
|
|
kdebug ("String: ")
|
|
kdebug_num (String::ID, 3)
|
|
kdebug ("\nWString: ")
|
|
kdebug_num (WString::ID, 3)
|
|
kdebug ("\nDevice: ")
|
|
kdebug_num (Device::ID, 3)
|
|
kdebug ("\nParent: ")
|
|
kdebug_num (Parent::ID, 3)
|
|
kdebug ("\nKeyboard: ")
|
|
kdebug_num (Keyboard::ID, 3)
|
|
kdebug ("\nBuzzer: ")
|
|
kdebug_num (Buzzer::ID, 3)
|
|
kdebug ("\nDisplay: ")
|
|
kdebug_num (Display::ID, 3)
|
|
kdebug ("\nSetting: ")
|
|
kdebug_num (Setting::ID, 3)
|
|
kdebug ("\nDirectory: ")
|
|
kdebug_num (Directory::ID, 3)
|
|
kdebug ("\nWDirectory: ")
|
|
kdebug_num (WDirectory::ID, 3)
|
|
kdebug ("\nFilesystem: ")
|
|
kdebug_num (Filesystem::ID, 3)
|
|
kdebug ("\nStream: ")
|
|
kdebug_num (Stream::ID, 3)
|
|
kdebug ("\n")
|
|
|
|
Kernel::Num start ():
|
|
// Wait for the debugging device to be active, in case there is one.
|
|
Kernel::schedule ()
|
|
init_alloc ()
|
|
dump_devices ()
|
|
top_memory = Kernel::get_top_memory ()
|
|
Directory root = receive_devices ()
|
|
root.lock_ro ()
|
|
kdebug_line ()
|
|
list_files (root)
|
|
kdebug_line ()
|
|
sort ()
|
|
kdebug_line ()
|
|
Kernel::Caps caps = Kernel::my_memory.create_caps (max_pages)
|
|
kdebug ("max pages: ")
|
|
kdebug_num (max_pages)
|
|
kdebug ("\n")
|
|
slot = caps.use ()
|
|
mapping = alloc_space (max_pages)
|
|
for unsigned i = 0; i < num_files; ++i:
|
|
kdebug_line ()
|
|
run (&files[index[i]], files[index[i]].name[0] == '#')
|
|
kdebug_line ()
|
|
kdebug_line ()
|
|
root.unlock_ro ()
|
|
kdebug_line ()
|
|
Kernel::free_slot (slot)
|
|
kdebug_line ()
|
|
Kernel::my_memory.destroy (caps)
|
|
kdebug_line ()
|
|
return 0
|