Removed posix container files, moved libl4 to conts, moving linux module to conts as well

This commit is contained in:
Bahadir Balban
2009-09-04 13:59:07 +03:00
parent ccd8a61a89
commit ece106e591
645 changed files with 2 additions and 78867 deletions

115
conts/SConscript Normal file
View File

@@ -0,0 +1,115 @@
# -*- mode: python; coding: utf-8; -*-
# Codezero -- a microkernel for embedded systems.
#
# Copyright © 2009 B Labs Ltd
#
# 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/>.
#
# Author: Russel Winder
Import('environment', 'startAxf')
posixServicesDirectory = 'posix'
compilationProducts = []
########## Build the task libraries ########################
taskSupportLibraryEnvironment = environment.Clone()
taskSupportLibraryEnvironment.Append(CPPPATH = ['#' + environment['buildDirectory'], '#' + environment['buildDirectory'] + '/l4', '#' + environment['includeDirectory']])
taskSupportLibraryEnvironment['posixServicesDirectory'] = 'containers/' + posixServicesDirectory
taskLibraryNames = [f.name for f in Glob(posixServicesDirectory + '/lib*')]
taskLibraries = []
for library in taskLibraryNames:
taskLibraries.append(SConscript(posixServicesDirectory + '/' + library + '/SConscript', exports = {'environment': taskSupportLibraryEnvironment}))
Alias ('tasklibs', taskLibraries)
environment['targetHelpEntries']['tasklibs'] = 'build the support libraries for the tasks in the containers.'
########## Build the tasks ########################
def buildTask(programName, sources, environment, previousImage, extraCppPath=None):
e = environment.Clone()
e.Append(LINKFLAGS=['-T' + e['posixServicesDirectory'] + '/' + programName + '/linker.lds'])
e.Append(LIBPATH=['#' + e['buildDirectory'] + '/' + e['posixServicesDirectory'] + '/' + programName])
if extraCppPath: e.Append(CPPPATH=extraCppPath)
objects = e.StaticObject(sources)
Depends(objects, e['configFiles'])
program = e.Program(programName, objects)
environment['physicalBaseLinkerScript'] = Command('physical_base.lds', previousImage, 'tools/pyelf/readelf.py --first-free-page ' + previousImage[0].path + ' >> $TARGET')
Depends(program, environment['physicalBaseLinkerScript'])
return program
tasksEnvironment = environment.Clone()
tasksEnvironment.Append(LIBS = taskLibraries + ['gcc'] + taskLibraries)
tasksEnvironment.Append(CPPDEFINES = ['__USERSPACE__'])
tasksEnvironment.Append(CPPPATH = ['#' + environment['buildDirectory'], '#' + environment['buildDirectory'] + '/l4', '#' + environment['includeDirectory'], 'include', \
'#containers/' +posixServicesDirectory + '/libl4/include', '#containers/' + posixServicesDirectory + '/libc/include', \
'#containers/' + posixServicesDirectory + '/libmem', '#containers/' + posixServicesDirectory + '/libposix/include'])
tasksEnvironment.Append(buildTask = buildTask)
tasksEnvironment['posixServicesDirectory'] = 'containers/' + posixServicesDirectory
####
#### TODO: Why does the linker require crt0.o to be in the current directory and named as such. Is it
#### because of the text in the linker script?
####
## If the ordering of tasks is not important then we should just be able to pull in all non-library
## directories that are not bootdesc. If there needs to be a policy then either the directory names need to
## provide an order or a policy file presenting an order is needed. For now we have a policy file. Since
## we need to guarantee the ordering across many containers and many builds.
## taskNameList = [ f.name for f in Glob(posixServicesDirectory + '*') if f.name not in taskLibraryNames + ['bootdesc'] ]
## imageOrderData = [(taskName, []) for taskName in taskNameList]
# To get the policy file, we have to know the build is being executed in the build/containers directory,
# and that we can't use '#' as this is Python not SCons.
####
#### TODO: Task order (mm0, fs0, test0) works, but no other order does. This implies a bug in the SCons
#### code.
####
execfile('../../containers/' + posixServicesDirectory + '/taskOrder.py')
imageOrderData = [(taskName, []) for taskName in taskOrder]
imageOrderData[0][1].append(startAxf)
tasks = []
for i in range(len(imageOrderData)):
taskName = imageOrderData[i][0]
dependency = imageOrderData[i][1]
program = SConscript(posixServicesDirectory + '/' + taskName + '/SConscript', exports = {'environment': tasksEnvironment, 'previousImage': dependency[0]})
tasks.append(program)
if i < len(imageOrderData) - 1:
imageOrderData[i+1][1].append(program)
Alias ('tasks', tasks)
environment['targetHelpEntries']['tasks'] = 'build the tasks for the containers.'
########## Create the boot description ########################
bootdescEnvironment = environment.Clone()
bootdescEnvironment['posixServicesDirectory'] = 'containers/' + posixServicesDirectory
bootdesc = SConscript(posixServicesDirectory + '/bootdesc/SConscript', exports = {'environment': bootdescEnvironment, 'images': [startAxf] + tasks})
Alias('bootdesc', bootdesc)
environment['targetHelpEntries']['bootdesc'] = 'build the boot descriptions of the tasks for the containers.'
compilationProducts = tasks + [bootdesc]
Return('compilationProducts')

34
conts/libl4/SConscript Normal file
View File

@@ -0,0 +1,34 @@
# -*- mode: python; coding: utf-8; -*-
# Codezero -- a microkernel for embedded systems.
#
# Copyright © 2009 B Labs Ltd
#
# 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/>.
#
# Author: Russel Winder
Import('environment')
e = environment.Clone()
e.Append(CPPPATH = ['include'])
# TODO: There are errors in this code that -Werror gives problems with.
e['CCFLAGS'] = ['-g', '-nostdlib', '-Wall', '-ffreestanding', '-std=gnu99']
objects = e.StaticObject(Glob('src/*.c') + Glob('src/' + e['ARCH'] + '/*.[cS]'))
Depends(objects, e['configFiles'])
library = e.StaticLibrary('l4', objects)
Depends(library, e['configFiles'])
Return('library')

View File

@@ -0,0 +1 @@
arch-arm

View File

@@ -0,0 +1,15 @@
#ifndef __ARM_ASM_H__
#define __ARM_ASM_H__
#define BEGIN_PROC(name) \
.global name; \
.type name,function; \
.align; \
name:
#define END_PROC(name) \
.fend_##name: \
.size name,.fend_##name - name;
#endif /* __ARM_ASM_H__ */

View File

@@ -0,0 +1,92 @@
/*
* System call prototypes.
*
* Copyright (C) 2007 Bahadir Balban
*/
#ifndef __ARM_SYSCALLS_H__
#define __ARM_SYSCALLS_H__
#include <l4lib/arch/types.h>
#include <l4lib/arch/utcb.h>
#include <l4/generic/space.h>
#include <l4/api/space.h>
#include <l4/api/kip.h>
#include <l4/api/ipc.h>
#include <l4/api/thread.h>
struct task_ids {
int tid;
int spid;
int tgid;
};
static inline void *
l4_kernel_interface(unsigned int *api_version, unsigned int *api_flags,
unsigned int *kernel_id)
{
return (void *)L4_KIP_ADDRESS;
}
typedef unsigned int (*__l4_thread_switch_t)(u32);
extern __l4_thread_switch_t __l4_thread_switch;
unsigned int l4_thread_switch (u32 dest);
typedef int (*__l4_getid_t)(struct task_ids *ids);
extern __l4_getid_t __l4_getid;
int l4_getid(struct task_ids *ids);
typedef int (*__l4_ipc_t)(l4id_t to, l4id_t from, u32 flags);
extern __l4_ipc_t __l4_ipc;
int l4_ipc(l4id_t to, l4id_t from, u32 flags);
typedef int (*__l4_capability_control_t)(unsigned int req, unsigned int flags, void *buf);
extern __l4_capability_control_t __l4_capability_control;
int l4_capability_control(unsigned int req, unsigned int flags, void *buf);
typedef int (*__l4_map_t)(void *phys, void *virt,
u32 npages, u32 flags, l4id_t tid);
extern __l4_map_t __l4_map;
int l4_map(void *p, void *v, u32 npages, u32 flags, l4id_t tid);
typedef int (*__l4_unmap_t)(void *virt, unsigned long npages, l4id_t tid);
extern __l4_unmap_t __l4_unmap;
int l4_unmap(void *virtual, unsigned long numpages, l4id_t tid);
typedef int (*__l4_thread_control_t)(unsigned int action, struct task_ids *ids);
extern __l4_thread_control_t __l4_thread_control;
int l4_thread_control(unsigned int action, struct task_ids *ids);
typedef int (*__l4_space_control_t)(unsigned int action, void *kdata);
extern __l4_space_control_t __l4_space_control;
int l4_space_control(unsigned int action, void *kdata);
typedef int (*__l4_ipc_control_t)(unsigned int action, l4id_t blocked_sender,
u32 blocked_tag);
extern __l4_ipc_control_t __l4_ipc_control;
int l4_ipc_control(unsigned int, l4id_t blocked_sender, u32 blocked_tag);
typedef int (*__l4_exchange_registers_t)(void *exregs_struct, l4id_t tid);
extern __l4_exchange_registers_t __l4_exchange_registers;
int l4_exchange_registers(void *exregs_struct, l4id_t tid);
typedef int (*__l4_container_control_t)(unsigned int req, unsigned int flags, void *buf);
extern __l4_container_control_t __l4_container_control;
int l4_container_control(unsigned int req, unsigned int flags, void *buf);
typedef int (*__l4_time_t)(void *timeval, int set);
extern __l4_time_t __l4_time;
int l4_time(void *timeval, int set);
typedef int (*__l4_mutex_control_t)(void *mutex_word, int op);
extern __l4_mutex_control_t __l4_mutex_control;
int l4_mutex_control(void *mutex_word, int op);
/* To be supplied by server tasks. */
void *virt_to_phys(void *);
void *phys_to_virt(void *);
#endif /* __ARM_SYSCALLS_H__ */

View File

@@ -0,0 +1,292 @@
/*
* Helper functions that wrap raw l4 syscalls.
*
* Copyright (C) 2007-2009 Bahadir Bilgehan Balban
*/
#ifndef __L4LIB_SYSLIB_H__
#define __L4LIB_SYSLIB_H__
#include <printf.h>
#include <l4lib/arch/syscalls.h>
#include <l4/macros.h>
/*
* NOTE:
* Its best to use these wrappers because they generalise the way
* common ipc data like sender id, error, ipc tag are passed
* between ipc parties.
*
* The arguments to l4_ipc() are used by the microkernel to initiate
* the ipc. Any data passed in message registers may or may not be
* a duplicate of this data, but the distinction is that anything
* that is passed via the mrs are meant to be used by the other party
* participating in the ipc.
*/
/* For system call arguments */
#define L4SYS_ARG0 (MR_UNUSED_START)
#define L4SYS_ARG1 (MR_UNUSED_START + 1)
#define L4SYS_ARG2 (MR_UNUSED_START + 2)
#define L4SYS_ARG3 (MR_UNUSED_START + 3)
#define L4_IPC_TAG_MASK 0x00000FFF
/*
* Servers get sender.
*/
static inline l4id_t l4_get_sender(void)
{
return (l4id_t)read_mr(MR_SENDER);
}
/*
* When doing an ipc the sender never has to be explicitly set in
* the utcb via this function since this information is found out
* by the microkernel by checking the system caller's id. This is
* only used for restoring the sender on the utcb in order to
* complete an earlier ipc.
*/
static inline void l4_set_sender(l4id_t sender)
{
write_mr(MR_SENDER, sender);
}
static inline unsigned int l4_set_ipc_size(unsigned int word, unsigned int size)
{
word &= ~L4_IPC_FLAGS_SIZE_MASK;
word |= ((size << L4_IPC_FLAGS_SIZE_SHIFT) & L4_IPC_FLAGS_SIZE_MASK);
return word;
}
static inline unsigned int l4_get_ipc_size(unsigned int word)
{
return (word & L4_IPC_FLAGS_SIZE_MASK) >> L4_IPC_FLAGS_SIZE_SHIFT;
}
static inline unsigned int l4_set_ipc_msg_index(unsigned int word, unsigned int index)
{
/* FIXME: Define MR_PRIMARY_TOTAL, MR_TOTAL etc. and use MR_TOTAL HERE! */
BUG_ON(index > UTCB_SIZE);
word &= ~L4_IPC_FLAGS_MSG_INDEX_MASK;
word |= (index << L4_IPC_FLAGS_MSG_INDEX_SHIFT) &
L4_IPC_FLAGS_MSG_INDEX_MASK;
return word;
}
static inline unsigned int l4_get_ipc_msg_index(unsigned int word)
{
return (word & L4_IPC_FLAGS_MSG_INDEX_MASK)
>> L4_IPC_FLAGS_MSG_INDEX_SHIFT;
}
static inline unsigned int l4_set_ipc_flags(unsigned int word, unsigned int flags)
{
word &= ~L4_IPC_FLAGS_TYPE_MASK;
word |= flags & L4_IPC_FLAGS_TYPE_MASK;
return word;
}
static inline unsigned int l4_get_ipc_flags(unsigned int word)
{
return word & L4_IPC_FLAGS_TYPE_MASK;
}
static inline unsigned int l4_get_tag(void)
{
return read_mr(MR_TAG) & L4_IPC_TAG_MASK;
}
static inline void l4_set_tag(unsigned int tag)
{
unsigned int tag_flags = read_mr(MR_TAG);
tag_flags &= ~L4_IPC_TAG_MASK;
tag_flags |= tag & L4_IPC_TAG_MASK;
write_mr(MR_TAG, tag_flags);
}
/*
* If we're about to do another ipc, this saves the last ipc's
* parameters such as the sender and tag information.
* Any previously saved data in save slots are destroyed.
*/
static inline void l4_save_ipcregs(void)
{
l4_get_utcb()->saved_sender = l4_get_sender();
l4_get_utcb()->saved_tag = l4_get_tag();
}
static inline void l4_restore_ipcregs(void)
{
l4_set_tag(l4_get_utcb()->saved_tag);
l4_set_sender(l4_get_utcb()->saved_sender);
}
static inline l4id_t self_tid(void)
{
struct task_ids ids;
l4_getid(&ids);
return ids.tid;
}
static inline int l4_send_full(l4id_t to, unsigned int tag)
{
l4_set_tag(tag);
return l4_ipc(to, L4_NILTHREAD, L4_IPC_FLAGS_FULL);
}
static inline int l4_receive_full(l4id_t from)
{
return l4_ipc(L4_NILTHREAD, from, L4_IPC_FLAGS_FULL);
}
static inline int l4_sendrecv_full(l4id_t to, l4id_t from, unsigned int tag)
{
int err;
BUG_ON(to == L4_NILTHREAD || from == L4_NILTHREAD);
l4_set_tag(tag);
err = l4_ipc(to, from, L4_IPC_FLAGS_FULL);
return err;
}
static inline int l4_send_extended(l4id_t to, unsigned int tag,
unsigned int size, void *buf)
{
unsigned int flags = 0;
l4_set_tag(tag);
/* Set up flags word for extended ipc */
flags = l4_set_ipc_flags(flags, L4_IPC_FLAGS_EXTENDED);
flags = l4_set_ipc_size(flags, size);
flags = l4_set_ipc_msg_index(flags, L4SYS_ARG0);
/* Write buffer pointer to MR index that we specified */
write_mr(L4SYS_ARG0, (unsigned long)buf);
return l4_ipc(to, L4_NILTHREAD, flags);
}
static inline int l4_receive_extended(l4id_t from, unsigned int size, void *buf)
{
unsigned int flags = 0;
/* Indicate extended receive */
flags = l4_set_ipc_flags(flags, L4_IPC_FLAGS_EXTENDED);
/* How much data is accepted */
flags = l4_set_ipc_size(flags, size);
/* Indicate which MR index buffer pointer is stored */
flags = l4_set_ipc_msg_index(flags, L4SYS_ARG0);
/* Set MR with buffer to receive data */
write_mr(L4SYS_ARG0, (unsigned long)buf);
return l4_ipc(L4_NILTHREAD, from, flags);
}
static inline int l4_sendrecv_extended(l4id_t to, l4id_t from,
unsigned int tag, void *buf)
{
/* Need to imitate sendrecv but with extended send/recv flags */
return 0;
}
static inline int l4_send(l4id_t to, unsigned int tag)
{
l4_set_tag(tag);
return l4_ipc(to, L4_NILTHREAD, 0);
}
static inline int l4_sendrecv(l4id_t to, l4id_t from, unsigned int tag)
{
int err;
BUG_ON(to == L4_NILTHREAD || from == L4_NILTHREAD);
l4_set_tag(tag);
err = l4_ipc(to, from, 0);
return err;
}
static inline int l4_receive(l4id_t from)
{
return l4_ipc(L4_NILTHREAD, from, 0);
}
/* Servers:
* Sets the message register for returning errors back to client task.
* These are usually posix error codes.
*/
static inline void l4_set_retval(int retval)
{
write_mr(MR_RETURN, retval);
}
/* Clients:
* Learn result of request.
*/
static inline int l4_get_retval(void)
{
return read_mr(MR_RETURN);
}
static inline void l4_print_mrs()
{
printf("Message registers: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
read_mr(0), read_mr(1), read_mr(2), read_mr(3),
read_mr(4), read_mr(5));
}
/* Servers:
* Return the ipc result back to requesting task.
*/
static inline int l4_ipc_return(int retval)
{
l4id_t sender = l4_get_sender();
l4_set_retval(retval);
/* Setting the tag may overwrite retval so we l4_send without tagging */
return l4_ipc(sender, L4_NILTHREAD, 0);
}
void *l4_new_virtual(int npages);
void *l4_del_virtual(void *virt, int npages);
/* A helper that translates and maps a physical address to virtual */
static inline void *l4_map_helper(void *phys, int npages)
{
struct task_ids ids;
void *virt = l4_new_virtual(npages);
l4_getid(&ids);
l4_map(phys, virt, npages, MAP_USR_RW_FLAGS, ids.tid);
return virt;
}
/* A helper that translates and maps a physical address to virtual */
static inline void *l4_unmap_helper(void *virt, int npages)
{
struct task_ids ids;
l4_getid(&ids);
l4_unmap(virt, npages, ids.tid);
l4_del_virtual(virt, npages);
return 0;
}
#endif /* __L4LIB_SYSLIB_H__ */

View File

@@ -0,0 +1,8 @@
#ifndef __L4LIB_ARM_TYPES_H___
#define __L4LIB_ARM_TYPES_H__
#define TASK_ID_INVALID -1
#include <l4/arch/arm/types.h>
#endif /* __L4LIB_ARM_TYPES_H__ */

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2009 Bahadir Bilgehan Balban
*/
#ifndef __ARM_UTCB_H__
#define __ARM_UTCB_H__
#define USER_UTCB_REF 0xFF000050
#define L4_KIP_ADDRESS 0xFF000000
#define UTCB_KIP_OFFSET 0x50
#ifndef __ASSEMBLY__
#include <l4lib/types.h>
#include <l4/macros.h>
#include INC_GLUE(message.h)
#include INC_GLUE(memory.h)
#include <string.h>
#include <stdio.h>
/* UTCB implementation */
/*
* NOTE: In syslib.h the first few mrs are used by data frequently
* needed for all ipcs. Those mrs are defined the kernel message.h
*/
struct utcb {
u32 mr[MR_TOTAL]; /* MRs that are mapped to real registers */
u32 saved_tag; /* Saved tag field for stacked ipcs */
u32 saved_sender; /* Saved sender field for stacked ipcs */
u32 mr_rest[MR_REST]; /* Complete the utcb for up to 64 words */
};
extern struct kip *kip;
/*
* Pointer to Kernel Interface Page's UTCB pointer offset.
*/
extern struct utcb **kip_utcb_ref;
static inline struct utcb *l4_get_utcb()
{
/*
* By double dereferencing, we get the private TLS (aka UTCB). First
* reference is to the KIP's utcb offset, second is to the utcb itself,
* to which the KIP's utcb reference had been updated during context
* switch.
*/
return *kip_utcb_ref;
}
/* Functions to read/write utcb registers */
static inline unsigned int read_mr(int offset)
{
if (offset < MR_TOTAL)
return l4_get_utcb()->mr[offset];
else
return l4_get_utcb()->mr_rest[offset - MR_TOTAL];
}
static inline void write_mr(unsigned int offset, unsigned int val)
{
if (offset < MR_TOTAL)
l4_get_utcb()->mr[offset] = val;
else
l4_get_utcb()->mr_rest[offset - MR_TOTAL] = val;
}
#endif /* !__ASSEMBLY__ */
#endif /* __ARM_UTCB_H__ */

View File

@@ -0,0 +1,24 @@
#ifndef __MM0_EXREGS_H__
#define __MM0_EXREGS_H__
#include <l4/api/exregs.h>
void exregs_set_stack(struct exregs_data *s, unsigned long sp);
void exregs_set_mr(struct exregs_data *s, int offset, unsigned long val);
void exregs_set_pc(struct exregs_data *s, unsigned long pc);
void exregs_set_pager(struct exregs_data *s, l4id_t pagerid);
void exregs_set_utcb(struct exregs_data *s, unsigned long virt);
/*
exregs_set_stack(unsigned long sp)
exregs_set_pc(unsigned long pc)
exregs_set_return(unsigned long retreg)
exregs_set_arg0(unsigned long arg0)
exregs_set_mr0(unsigned long mr0)
exregs_set_mr_sender(unsigned long sender)
exregs_set_mr_return(unsigned long retreg)
exregs_set_all(unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3,
unsigned long sp, unsigned long pc, u32 valid_vector, l4id_t pager);
*/
#endif /* __MM0_EXREGS_H__ */

View File

@@ -0,0 +1,6 @@
#ifndef __L4LIB_INIT__
#define __L4LIB_INIT__
void __l4_init(void);
#endif

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2007, 2008 Bahadir Balban
*
* This file contains ipc definitions that are needed for server tasks
* to communicate with each other. For example common shared memory ids
* between two servers, or common ipc tags used between two servers are
* defined here.
*/
#ifndef __IPCDEFS_H__
#define __IPCDEFS_H__
#include <l4/api/ipc.h>
/* SHMID used betweeen FS0 and BLKDEV0 servers */
#define FS_BLKDEV_SHMID 0
/*** IPC Tags used between server tasks ***/
/* For ping ponging */
#define L4_IPC_TAG_SYNC_EXTENDED 1
#define L4_IPC_TAG_SYNC_FULL 2
#define L4_IPC_TAG_SYNC 3
/* To obtain default shared page address */
#define L4_IPC_TAG_SHPAGE 4
/* XXX: unused */
#define L4_IPC_TAG_GRANT 5
/* Posix system call tags */
#define L4_IPC_TAG_SHMGET 6
#define L4_IPC_TAG_SHMAT 7
#define L4_IPC_TAG_SHMDT 8
#define L4_IPC_TAG_MMAP 9
#define L4_IPC_TAG_MUNMAP 10
#define L4_IPC_TAG_MSYNC 11
#define L4_IPC_TAG_OPEN 12
#define L4_IPC_TAG_READ 13
#define L4_IPC_TAG_WRITE 14
#define L4_IPC_TAG_LSEEK 15
#define L4_IPC_TAG_CLOSE 16
#define L4_IPC_TAG_BRK 17
#define L4_IPC_TAG_READDIR 18
#define L4_IPC_TAG_MKDIR 19
#define L4_IPC_TAG_EXECVE 20
#define L4_IPC_TAG_CHDIR 21
#define L4_IPC_TAG_FORK 22
#define L4_IPC_TAG_STAT 23
#define L4_IPC_TAG_FSTAT 24
#define L4_IPC_TAG_FSYNC 25
#define L4_IPC_TAG_CLONE 26
#define L4_IPC_TAG_EXIT 27
#define L4_IPC_TAG_WAIT 28
/* Tags for ipc between fs0 and mm0 */
#define L4_IPC_TAG_TASKDATA 40
#define L4_IPC_TAG_PAGER_OPEN 41 /* vfs sends the pager open file data. */
#define L4_IPC_TAG_PAGER_READ 42 /* Pager reads file contents from vfs */
#define L4_IPC_TAG_PAGER_WRITE 43 /* Pager writes file contents to vfs */
#define L4_IPC_TAG_PAGER_CLOSE 44 /* Pager notifies vfs of file close */
#define L4_IPC_TAG_PAGER_UPDATE_STATS 45 /* Pager updates file stats in vfs */
#define L4_IPC_TAG_NOTIFY_FORK 46 /* Pager notifies vfs of process fork */
#define L4_IPC_TAG_NOTIFY_EXIT 47 /* Pager notifies vfs of process exit */
#define L4_IPC_TAG_PAGER_OPEN_BYPATH 48 /* Pager opens a vfs file by pathname */
#endif /* __IPCDEFS_H__ */

View File

@@ -0,0 +1,15 @@
/*
* Kernel Interface Page
*
* Copyright (C) 2007 Bahadir Balban
*
*/
#ifndef __L4LIB_KIP_H__
#define __L4LIB_KIP_H__
/* Use the kernel header */
#include <l4lib/types.h>
#include <l4lib/arch/syscalls.h>
#include <l4/api/kip.h>
#endif /* __KIP_H__ */

View File

@@ -0,0 +1,38 @@
/*
* User space locking
*
* Copyright (C) 2009 Bahadir Bilgehan Balban
*/
#ifndef __L4_MUTEX_H__
#define __L4_MUTEX_H__
#if !defined(__ASSEMBLY__)
#include <l4/api/mutex.h>
struct l4_mutex {
unsigned int lock;
} __attribute__((aligned(sizeof(int))));
void l4_mutex_init(struct l4_mutex *m);
int l4_mutex_lock(struct l4_mutex *m);
int l4_mutex_unlock(struct l4_mutex *m);
#endif
/* Mutex return value - don't mix up with mutes state */
#define L4_MUTEX_CONTENDED -1
#define L4_MUTEX_SUCCESS 0
/* Mutex states - Any valid tid value is a locked state */
#define L4_MUTEX_UNLOCKED -1
#define L4_MUTEX(m) \
struct l4_mutex m = { L4_MUTEX_UNLOCKED }
#endif /* __L4_MUTEX_H__ */

View File

@@ -0,0 +1,23 @@
#ifndef __OS_KSTAT_H__
#define __OS_KSTAT_H__
#include <l4lib/types.h>
/*
* Internal codezero-specific stat structure.
* This is converted to posix stat in userspace
*/
struct kstat {
u64 vnum;
u32 mode;
int links;
u16 uid;
u16 gid;
u64 size;
int blksize;
u64 atime;
u64 mtime;
u64 ctime;
};
#endif

View File

@@ -0,0 +1,7 @@
#ifndef __OS_READDIR_H__
#define __OS_READDIR_H__
/* Any os syscall related data that is not in posix */
ssize_t os_readdir(int fd, void *buf, size_t count);
#endif

View File

@@ -0,0 +1,14 @@
#ifndef __L4_THREAD_H__
#define __L4_THREAD_H__
#include <libl4/arch/utcb.h>
#include <libl4/arch/types.h>
struct l4_thread_struct {
l4id_t tlid; /* Thread local id */
struct task_ids ids; /* Thread L4-defined ids */
struct utcb *utcb; /* Thread utcb */
unsigned long stack_start; /* Thread start of stack */
};
#endif /* __L4_THREAD_H__ */

View File

@@ -0,0 +1,6 @@
#ifndef __TYPES_H__
#define __TYPES_H__
#include <l4lib/arch/types.h>
#endif /* __TYPES_H__ */

View File

@@ -0,0 +1,12 @@
/*
* Copyright (C) 2007 Bahadir Balban
*/
#ifndef __UTCB_H__
#define __UTCB_H__
#include <l4lib/types.h>
#include <l4lib/arch/utcb.h>
int utcb_init(void);
#endif /* __UTCB_H__ */

View File

@@ -0,0 +1,51 @@
/*
* Generic to arch-specific interface for
* exchange_registers()
*
* Copyright (C) 2008 Bahadir Balban
*/
#include <l4/macros.h>
#include <l4lib/exregs.h>
#include INC_GLUE(message.h)
void exregs_set_mr(struct exregs_data *s, int offset, unsigned long val)
{
/* Get MR0 */
u32 *mr = &s->context.MR0_REGISTER;
/* Sanity check */
BUG_ON(offset > MR_TOTAL || offset < 0);
/* Set MR */
mr[offset] = val;
/* Set valid bit for mr register */
s->valid_vect |= FIELD_TO_BIT(exregs_context_t, MR0_REGISTER) << offset;
}
void exregs_set_pager(struct exregs_data *s, l4id_t pagerid)
{
s->pagerid = pagerid;
s->flags |= EXREGS_SET_PAGER;
}
void exregs_set_utcb(struct exregs_data *s, unsigned long virt)
{
s->utcb_address = virt;
s->flags |= EXREGS_SET_UTCB;
}
void exregs_set_stack(struct exregs_data *s, unsigned long sp)
{
s->context.sp = sp;
s->valid_vect |= FIELD_TO_BIT(exregs_context_t, sp);
}
void exregs_set_pc(struct exregs_data *s, unsigned long pc)
{
s->context.pc = pc;
s->valid_vect |= FIELD_TO_BIT(exregs_context_t, pc);
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2009 Bahadir Balban
*/
#include <l4lib/arch/asm.h>
#include <l4lib/mutex.h>
/*
* NOTES:
*
* Recap on swp:
*
* swp rx, ry, [rz]
*
* In one instruction:
*
* 1) Stores the value in ry into location pointed by rz.
* 2) Loads the value in the location of rz into rx.
* By doing so, in one instruction one can attempt to lock
* a word, and discover whether it was already locked.
*
* Why use tid of thread to lock mutex instead of
* a single lock value?
*
* Because in one atomic instruction, not only the locking attempt
* should be able to indicate whether it is locked, but also
* the contentions. A unified lock value would not be sufficient.
* The only way to indicate a contended lock is to store the
* unique TID of the locker.
*/
/*
* Any non-negative value that is a potential TID
* (including 0) means mutex is locked.
*/
/*
* @r0 = address of mutex word
* @r1 = unique tid of current thread
*/
BEGIN_PROC(__l4_mutex_lock)
swp r2, r1, [r0]
cmp r2, #L4_MUTEX_UNLOCKED @ Was the lock available?
movne r0, #L4_MUTEX_CONTENDED @ Indicate failure
moveq r0, #L4_MUTEX_SUCCESS @ Indicate success
mov pc, lr
END_PROC(__l4_mutex_lock)
/*
* @r0 = address of mutex word
* @r1 = unique tid of current thread
*/
BEGIN_PROC(__l4_mutex_unlock)
mov r3, #L4_MUTEX_UNLOCKED
swp r2, r3, [r0]
cmp r2, r1 @ Check lock had original tid value
movne r0, #L4_MUTEX_CONTENDED @ Indicate contention
moveq r0, #L4_MUTEX_SUCCESS @ Indicate no contention
cmp r2, #L4_MUTEX_UNLOCKED @ Or - was it already unlocked?
1:
beq 1b @ If so busy-spin to indicate bug.
mov pc, lr
END_PROC(__l4_mutex_unlock)

View File

@@ -0,0 +1,222 @@
/*
* Userspace system call interface.
*
* Copyright (C) 2007 Bahadir Balban
*/
#include <l4lib/arch/asm.h>
#include <l4lib/arch/utcb.h>
#include <l4/generic/space.h>
#include <l4/macros.h>
#include INC_GLUE(message.h)
/* Old macro */
#if 0
.macro utcb_address rx
ldr \rx, =utcb
.endm
#endif
/* New macro does double dereference */
.macro utcb_address rx
ldr \rx, =kip_utcb_ref @ First get pointer to utcb pointer in KIP
ldr \rx, [\rx] @ Get pointer to UTCB address from UTCB pointer in KIP
ldr \rx, [\rx] @ Get the utcb address
.endm
BEGIN_PROC(l4_thread_switch)
ldr r12, =__l4_thread_switch
ldr pc, [r12] @ Jump into the SWI. Kernel returns to LR_USR, which is the caller.
END_PROC(l4_thread_switch)
/*
* The syscall returns process ids. This function saves the returned values in the
* arguments passed by reference. @r0 = struct task_ids *
*/
BEGIN_PROC(l4_getid)
ldr r12, =__l4_getid @ See l4_kdata_read for why its so simple.
ldr pc, [r12] @ Return.
END_PROC(l4_getid)
/*
* Reads/manipulates capabilities of a thread, particularly a pager.
* @r0 = request type, @r1 = request flags, @r2 = io buffer ptr
*/
BEGIN_PROC(l4_capability_control)
ldr r12, =__l4_capability_control
ldr pc, [r12] @ Jump into the SWI
/*
* The LR_USR points at the return address of this function. The system
* call return path directly jumps to LR_USR so we don't even need a
* return instruction here.
*/
END_PROC(l4_capability_control)
/*
* For clone() we need special assembler handling
* Same signature as ipc(): @r0 = to, @r1 = from @r2 = flags
*
* NOTE: Note that this breaks l4 system call interface,
* this should be moved elsewhere and modified using existing l4 mechanisms.
*/
BEGIN_PROC(arch_clone)
stmfd sp!, {r4-r8,lr} @ Save context.
utcb_address r12 @ Get utcb address.
ldmia r12!, {r3-r8} @ Load 6 Message registers from utcb. MR0-MR5
ldr r12, =__l4_ipc
mov lr, pc
ldr pc, [r12] @ Perform the ipc()
/*
* At this moment:
* - MR_RETURN tells us whether we are parent or child (or have failed).
* - Child has new SP set, with |func_ptr|arg1|{End of stack}SP<-| on stack.
* - Child needs exit logic when its function is finished.
*/
cmp r0, #0 @ Check ipc success
blt ipc_failed
cmp MR_RETURN_REGISTER, #0 @ Check ipc return register MR_RETURN.
blt clone_failed @ Ipc was ok but clone() failed.
bgt parent_return @ It has child pid, goto parent return.
child:
ldr r0, [sp, #-4]! @ Load child's first argument.
mov lr, pc @ Save return address
ldr pc, [sp, #-4]! @ Load function pointer from stack
child_exit:
b child_exit @ We infinitely loop for now.
@ Return with normal ipc return sequence
parent_return:
clone_failed:
ipc_failed:
utcb_address r12 @ Get utcb
stmia r12, {r3-r8} @ Store mrs.
ldmfd sp!, {r4-r8,pc} @ Return restoring pc and context.
END_PROC(arch_clone)
/*
* Inter-process communication. Loads message registers as arguments before the call,
* and stores them as results after the call. @r0 = to, @r1 = from.
*/
BEGIN_PROC(l4_ipc)
stmfd sp!, {r4-r8,lr} @ Save context.
utcb_address r12 @ Get utcb address.
ldmia r12!, {r3-r8} @ Load 6 Message registers from utcb. MR0-MR5
ldr r12, =__l4_ipc
mov lr, pc
ldr pc, [r12]
utcb_address r12 @ Get utcb address.
stmia r12, {r3-r8} @ Store 6 Message registers to utcb. MR0-MR5
ldmfd sp!, {r4-r8,pc} @ Return restoring pc, and context.
END_PROC(l4_ipc)
/*
* System call that maps an area of memory into the given address space.
* @r0 = physical address, @r1 = virtual address, @r2 = map size in pages,
* @r3 = map flags, @r4 = The tgid of the address space to map.
*/
BEGIN_PROC(l4_map)
stmfd sp!, {r4, lr}
ldr r4, [sp, #8] @ FIXME: Is this right?
ldr r12, =__l4_map
mov lr, pc @ We must return here to restore r4.
ldr pc, [r12]
ldmfd sp!, {r4, pc}
END_PROC(l4_map)
/*
* System call that unmaps an area of memory into the given address space.
* @r0 = virtual, @r1 = pages, @r2 = tid of address space to unmap
*/
BEGIN_PROC(l4_unmap)
stmfd sp!, {lr}
ldr r12, =__l4_unmap
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_unmap)
/*
* System call that controls containers and their parameters.
* @r0 = request type, @r1 = request flags, @r2 = io buffer ptr
*/
BEGIN_PROC(l4_container_control)
stmfd sp!, {lr}
ldr r12, =__l4_container_control
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_container_control)
/*
* System call that gets or sets the time info structure.
* @r0 = ptr to time structure @r1 = set or get. set = 1, get = 0.
*/
BEGIN_PROC(l4_time)
stmfd sp!, {lr}
ldr r12, =__l4_time
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_time)
/*
* System call that controls thread creation, destruction and modification.
* @r0 = thread action, @r1 = &ids, @r2 = utcb address
*/
BEGIN_PROC(l4_thread_control)
stmfd sp!, {lr}
ldr r12, =__l4_thread_control
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_thread_control)
/*
* System call that modifies ipc blocked sender lists of receivers.
* @r0 = Action (e.g. block/unblock), @r1 = sender id, @r2 = sender tag
*/
BEGIN_PROC(l4_ipc_control)
stmfd sp!, {lr}
ldr r12, =__l4_ipc_control
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_ipc_control)
/*
* Manipulates address spaces, e.g. sets up shared memory areas between threads
* @r0 = operation code, @r1 = struct shm_kdata *kdata
*/
BEGIN_PROC(l4_space_control)
stmfd sp!, {lr}
ldr r12, =__l4_space_control
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_space_control)
/*
* Locks/unlocks a userspace mutex.
* @r0 = mutex virtual address, @r1 = mutex operation code
*/
BEGIN_PROC(l4_mutex_control)
stmfd sp!, {lr}
ldr r12, =__l4_mutex_control
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_mutex_control)
/*
* Sets registers of a thread and its pager.
* @r0 = ptr to exregs_data structure, @r1 = tid of thread.
*/
BEGIN_PROC(l4_exchange_registers)
stmfd sp!, {lr}
ldr r12, =__l4_exchange_registers
mov lr, pc
ldr pc, [r12]
ldmfd sp!, {pc} @ Restore original lr and return.
END_PROC(l4_exchange_registers)

62
conts/libl4/src/init.c Normal file
View File

@@ -0,0 +1,62 @@
/*
* Initialise system call offsets and utcb reference.
*
* Copyright (C) 2007-2009 Bahadir Bilgehan Balban
*/
#include <l4lib/kip.h>
#include <l4lib/arch/syslib.h>
#include <l4lib/arch/utcb.h>
#include <l4lib/ipcdefs.h>
#include <l4/macros.h>
#include INC_GLUE(memlayout.h)
#include <stdio.h>
__l4_ipc_t __l4_ipc = 0;
__l4_map_t __l4_map = 0;
__l4_unmap_t __l4_unmap = 0;
__l4_getid_t __l4_getid = 0;
__l4_thread_switch_t __l4_thread_switch = 0;
__l4_thread_control_t __l4_thread_control = 0;
__l4_ipc_control_t __l4_ipc_control = 0;
__l4_space_control_t __l4_space_control = 0;
__l4_exchange_registers_t __l4_exchange_registers = 0;
__l4_container_control_t __l4_container_control = 0;
__l4_capability_control_t __l4_capability_control = 0;
__l4_time_t __l4_time = 0;
__l4_mutex_control_t __l4_mutex_control = 0;
struct kip *kip;
/*
* Reference to private UTCB of this thread.
* Used only for pushing/reading ipc message registers.
*/
struct utcb **kip_utcb_ref;
void __l4_init(void)
{
/* Kernel interface page */
kip = l4_kernel_interface(0, 0, 0);
/* Reference to utcb field of KIP */
kip_utcb_ref = (struct utcb **)&kip->utcb;
__l4_ipc = (__l4_ipc_t)kip->ipc;
__l4_map = (__l4_map_t)kip->map;
__l4_unmap = (__l4_unmap_t)kip->unmap;
__l4_getid = (__l4_getid_t)kip->getid;
__l4_thread_switch = (__l4_thread_switch_t)kip->thread_switch;
__l4_thread_control= (__l4_thread_control_t)kip->thread_control;
__l4_ipc_control= (__l4_ipc_control_t)kip->ipc_control;
__l4_space_control= (__l4_space_control_t)kip->space_control;
__l4_exchange_registers =
(__l4_exchange_registers_t)kip->exchange_registers;
__l4_capability_control =
(__l4_capability_control_t)kip->capability_control;
__l4_container_control =
(__l4_container_control_t)kip->container_control;
__l4_time = (__l4_time_t)kip->time;
__l4_mutex_control = (__l4_mutex_control_t)kip->mutex_control;
}

89
conts/libl4/src/mutex.c Normal file
View File

@@ -0,0 +1,89 @@
/*
* Userspace mutex implementation
*
* Copyright (C) 2009 Bahadir Bilgehan Balban
*/
#include <l4lib/mutex.h>
#include <l4lib/types.h>
#include <l4lib/arch/syscalls.h>
#include <l4lib/arch/syslib.h>
/*
* NOTES:
*
* The design is kept as simple as possible.
*
* l4_mutex_lock() locks an initialized, mutex.
* If it contends, it calls the mutex syscall.
*
* l4_mutex_unlock() releases an acquired mutex.
* If there was contention, mutex syscall is called
* to resolve by the kernel.
*
* Internals:
*
* (1) The kernel creates a waitqueue for every unique
* mutex in the system, i.e. every unique physical
* address that is contended as a mutex. In that respect
* virtual mutex addresses are translated to physical
* and checked for match.
*
* (2) If a mutex is contended, kernel is called by both the
* locker and the unlocker (i.e. the lock holder). The syscall
* results in a rendezvous and both tasks quit the syscall
* synchronised. A rendezvous is necessary because it is not possible
* to check lock status and send a WAIT or WAKEUP request to the
* kernel atomically from userspace. In other words, a WAKEUP call
* would be lost if it arrived before the unsuccessful lock attempt
* resulted in a WAIT.
*
* (3) The unlocker releases the lock after it returns from the syscall.
* (4) The locker continuously tries to acquire the lock
*
* Issues:
* - The kernel action is to merely wake up sleepers. If
* a new thread acquires the lock meanwhile, all those woken
* up threads would have to sleep again.
* - All sleepers are woken up (aka thundering herd). This
* must be done because if a single task is woken up, there
* is no guarantee that that would in turn wake up others.
* It might even quit attempting to take the lock.
* - Whether this is the best design - time will tell.
*/
extern int __l4_mutex_lock(void *word, l4id_t tid);
extern int __l4_mutex_unlock(void *word, l4id_t tid);
void l4_mutex_init(struct l4_mutex *m)
{
m->lock = L4_MUTEX_UNLOCKED;
}
int l4_mutex_lock(struct l4_mutex *m)
{
l4id_t tid = self_tid();
int err;
while(__l4_mutex_lock(m, tid) == L4_MUTEX_CONTENDED) {
if ((err = l4_mutex_control(&m->lock, L4_MUTEX_LOCK)) < 0) {
printf("%s: Error: %d\n", __FUNCTION__, err);
return err;
}
}
return 0;
}
int l4_mutex_unlock(struct l4_mutex *m)
{
l4id_t tid = self_tid();
int err;
if (__l4_mutex_unlock(m, tid) == L4_MUTEX_CONTENDED) {
if ((err = l4_mutex_control(&m->lock, L4_MUTEX_UNLOCK)) < 0) {
printf("%s: Error: %d\n", __FUNCTION__, err);
return err;
}
}
return 0;
}