Revert "Merge branch 'libl4thread' of git://www.b-labs.co.uk/bora/git/codezero into bora"

This reverts commit 3f870b540f.
This commit is contained in:
Bahadir Balban
2009-11-05 19:17:36 +02:00
parent e28658c10e
commit f7565118f1
39 changed files with 65 additions and 1051 deletions

View File

@@ -1,57 +0,0 @@
/*
* This module allocates an unused address range from
* a given memory region defined as the pool range.
*
* Copyright (C) 2007 Bahadir Balban
*/
#include <addr.h>
#include <stdio.h>
/*
* Initializes an address pool, but uses an already
* allocated id pool for it.
*/
int address_pool_init_with_idpool(struct address_pool *pool,
struct id_pool *idpool,
unsigned long start, unsigned long end)
{
pool->idpool = idpool;
pool->start = start;
pool->end = end;
return 0;
}
int address_pool_init(struct address_pool *pool,
unsigned long start, unsigned long end,
int size)
{
if ((pool->idpool = id_pool_new_init((end - start) / size)) < 0)
return (int)pool->idpool;
pool->start = start;
pool->end = end;
return 0;
}
void *address_new(struct address_pool *pool, int npages)
{
unsigned int pfn;
if ((int)(pfn = ids_new_contiguous(pool->idpool, npages)) < 0)
return 0;
return (void *)__pfn_to_addr(pfn) + pool->start;
}
int address_del(struct address_pool *pool, void *addr, int npages)
{
unsigned long pfn = __pfn(page_align(addr) - pool->start);
if (ids_del_contiguous(pool->idpool, pfn, npages) < 0) {
printf("%s: Invalid address range returned to "
"virtual address pool.\n", __FUNCTION__);
return -1;
}
return 0;
}

View File

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

View File

@@ -1,11 +0,0 @@
#include <l4lib/arch/asm.h>
BEGIN_PROC(setup_new_thread)
ldr r0, [sp], #-4 @ Load first argument.
mov lr, pc @ Save return address
ldr pc, [sp], #-4 @ Load function pointer from stack
new_thread_exit:
b new_thread_exit @ We infinitely loop for now.
END_PROC(setup_new_thread)

View File

@@ -1,109 +0,0 @@
/*
* Bit manipulation functions.
*
* Copyright (C) 2007 Bahadir Balban
*/
#include <bit.h>
#include <stdio.h>
#include INC_GLUE(memory.h)
/* Emulation of ARM's CLZ (count leading zeroes) instruction */
unsigned int __clz(unsigned int bitvector)
{
unsigned int x = 0;
while((!(bitvector & ((unsigned)1 << 31))) && (x < 32)) {
bitvector <<= 1;
x++;
}
return x;
}
int find_and_set_first_free_bit(u32 *word, unsigned int limit)
{
int success = 0;
int i;
for(i = 0; i < limit; i++) {
/* Find first unset bit */
if (!(word[BITWISE_GETWORD(i)] & BITWISE_GETBIT(i))) {
/* Set it */
word[BITWISE_GETWORD(i)] |= BITWISE_GETBIT(i);
success = 1;
break;
}
}
/* Return bit just set */
if (success)
return i;
else
return -1;
}
int find_and_set_first_free_contig_bits(u32 *word, unsigned int limit,
int nbits)
{
int i = 0, first = 0, last = 0, found = 0;
/* Can't allocate more than the limit */
if (nbits > limit)
return -1;
/* This is a state machine that checks n contiguous free bits. */
/* FIXME: It should be <= instead of <. Fix & test in a single patch */
while (i + nbits < limit) {
first = i;
last = i;
while (!(word[BITWISE_GETWORD(last)] & BITWISE_GETBIT(last))) {
last++;
i++;
if (last == first + nbits) {
found = 1;
break;
}
}
if (found)
break;
i++;
}
/* If found, set the bits */
if (found) {
for (int x = first; x < first + nbits; x++)
word[BITWISE_GETWORD(x)] |= BITWISE_GETBIT(x);
return first;
} else
return -1;
}
int check_and_clear_bit(u32 *word, int bit)
{
/* Check that bit was set */
if (word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit)) {
word[BITWISE_GETWORD(bit)] &= ~BITWISE_GETBIT(bit);
return 0;
} else {
printf("Trying to clear already clear bit\n");
return -1;
}
}
int check_and_set_bit(u32 *word, int bit)
{
/* Check that bit was clear */
if (!(word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit))) {
word[BITWISE_GETWORD(bit)] |= BITWISE_GETBIT(bit);
return 0;
} else {
//printf("Trying to set already set bit\n");
return -1;
}
}
int check_and_clear_contig_bits(u32 *word, int first, int nbits)
{
for (int i = first; i < first + nbits; i++)
if (check_and_clear_bit(word, i) < 0)
return -1;
return 0;
}

View File

@@ -1,86 +0,0 @@
/*
* Used for thread and space ids, and also for utcbs.
*
* Copyright (C) 2009 B Labs Ltd.
*/
#include <idpool.h>
#include <stdio.h>
#include <l4/api/errno.h>
#include <malloc/malloc.h>
struct id_pool *id_pool_new_init(int totalbits)
{
int nwords = BITWISE_GETWORD(totalbits) + 1;
struct id_pool *new = kzalloc((nwords * SZ_WORD)
+ sizeof(struct id_pool));
if (!new)
return PTR_ERR(-ENOMEM);
new->nwords = nwords;
new->bitlimit = totalbits;
return new;
}
/* Search for a free slot up to the limit given */
int id_new(struct id_pool *pool)
{
return find_and_set_first_free_bit(pool->bitmap, pool->bitlimit);
}
/* This finds n contiguous free ids, allocates and returns the first one */
int ids_new_contiguous(struct id_pool *pool, int numids)
{
int id = find_and_set_first_free_contig_bits(pool->bitmap,
pool->bitlimit,
numids);
if (id < 0)
printf("%s: Warning! New id alloc failed\n", __FUNCTION__);
return id;
}
/* This deletes a list of contiguous ids given the first one and number of ids */
int ids_del_contiguous(struct id_pool *pool, int first, int numids)
{
int ret;
if (pool->nwords * WORD_BITS < first + numids)
return -1;
if ((ret = check_and_clear_contig_bits(pool->bitmap, first, numids)))
printf("%s: Error: Invalid argument range.\n", __FUNCTION__);
return ret;
}
int id_del(struct id_pool *pool, int id)
{
int ret;
if (pool->nwords * WORD_BITS < id)
return -1;
if ((ret = check_and_clear_bit(pool->bitmap, id) < 0))
printf("%s: Error: Could not delete id.\n", __FUNCTION__);
return ret;
}
/* Return a specific id, if available */
int id_get(struct id_pool *pool, int id)
{
int ret;
ret = check_and_set_bit(pool->bitmap, id);
if (ret < 0)
return ret;
else
return id;
}
int id_is_empty(struct id_pool *pool)
{
for (int i = 0; i < pool->nwords; i++)
if (pool->bitmap[i])
return 0;
return 1;
}

View File

@@ -1,146 +0,0 @@
/*
* Thread creation userspace helpers
*
* Copyright (C) 2009 B Labs Ltd.
*/
#include <stdio.h>
#include <l4lib/arch/syscalls.h>
#include <l4lib/exregs.h>
#include <l4/api/thread.h>
#include <l4/api/errno.h>
#include <utcb.h>
/* Symbolic constants and macros */
#define STACK_PTR(addr) align((addr - 1), 8)
#define IS_STACK_SETUP() (lib_stack_size)
/* Extern declarations */
extern void setup_new_thread(void);
/* Static variable definitions */
static unsigned long lib_stack_top_addr;
static unsigned long lib_stack_bot_addr;
static unsigned long lib_stack_size;
/* Function definitions */
int set_stack_params(unsigned long stack_top_addr,
unsigned long stack_bottom_addr,
unsigned long stack_size)
{
/* Ensure that arguments are valid. */
if (IS_STACK_SETUP()) {
printf("libl4thread: You have already called: %s. Simply, "
"this will have no effect!\n", __FUNCTION__);
return -EPERM;
}
if (!stack_top_addr || !stack_bottom_addr) {
printf("libl4thread: stack address range cannot contain "
"0x00000000 as a start and/or end address(es)!\n");
return -EINVAL;
}
// FIXME: Aligning should be taken into account.
/*
* Stack grows downward so the top of the stack will have
* the lowest numbered address.
*/
if (stack_top_addr >= stack_bottom_addr) {
printf("libl4thread: Stack bottom address must be bigger "
"than stack top address!\n");
return -EINVAL;
}
if (!stack_size) {
printf("libl4thread: Stack size cannot be zero!\n");
return -EINVAL;
}
/* stack_size at least must be equal to the difference. */
if ((stack_bottom_addr - stack_top_addr) < stack_size) {
printf("libl4thread: the given range size is lesser than "
"the stack size(0x%x)!\n", stack_size);
return -EINVAL;
}
/* Arguments passed the validity tests. */
/* Initialize internal variables */
lib_stack_bot_addr = stack_bottom_addr;
lib_stack_top_addr = stack_top_addr;
lib_stack_size = stack_size;
return 0;
}
int thread_create(struct task_ids *ids, unsigned int flags,
int (*func)(void *), void *arg)
{
struct exregs_data exregs;
unsigned long utcb_addr;
int err;
/* A few controls before granting access to thread creation */
if (!IS_STACK_SETUP() || !IS_UTCB_SETUP()) {
printf("libl4thread: Stack and/or utcb have not been set up. "
"Before calling thread_create, set_stack_params "
"and/or set_utcb_params have to be called with valid "
"arguments!\n");
return -EPERM;
}
/* Is there enough stack space for the new thread? */
if (lib_stack_top_addr >= lib_stack_bot_addr) {
printf("libl4thread: no stack space left!\n");
return -ENOMEM;
}
if (!(TC_SHARE_SPACE & flags)) {
printf("libl4thread: SAME address space is supported, which "
"means the only way to create a thread is with "
"TC_SHARE_SPACE flag. Other means of creating a "
"thread have not been supported yet!\n");
return -EINVAL;
}
/* Get a utcb addr for this thread */
if (!(utcb_addr = get_utcb_addr())) {
printf("libl4thread: No utcb address left!\n");
return -ENOMEM;
}
/* Get parent's ids */
l4_getid(ids);
/* Create thread */
if ((err = l4_thread_control(THREAD_CREATE | flags, ids)) < 0) {
printf("libl4thread: l4_thread_control(THREAD_CREATE) "
"failed with (%d)!\n", err);
return err;
}
/* Setup new thread pc, sp, utcb */
memset(&exregs, 0, sizeof(exregs));
exregs_set_stack(&exregs, STACK_PTR(lib_stack_bot_addr));
exregs_set_pc(&exregs, (unsigned long)setup_new_thread);
exregs_set_utcb(&exregs, (unsigned long)utcb_addr);
if ((err = l4_exchange_registers(&exregs, ids->tid)) < 0) {
printf("libl4thread: l4_exchange_registers failed with "
"(%d)!\n", err);
return err;
}
/* First word of new stack is arg */
((unsigned long *)STACK_PTR(lib_stack_bot_addr))[0] =
(unsigned long)arg;
/* Second word of new stack is function address */
((unsigned long *)STACK_PTR(lib_stack_bot_addr))[-1] =
(unsigned long)func;
/* Update the stack address */
lib_stack_bot_addr -= lib_stack_size;
/* Start the new thread */
if ((err = l4_thread_control(THREAD_RUN, ids)) < 0) {
printf("libl4thread: l4_thread_control(THREAD_RUN) "
"failed with (%d)!\n", err);
return err;
}
return 0;
}

View File

@@ -1,89 +0,0 @@
/*
* UTCB management in Codezero
*
* Copyright © 2009 B Labs Ltd
*/
#include <stdio.h>
#include <addr.h>
#include <utcb-common.h>
#include <malloc/malloc.h>
/* Globally disjoint utcb virtual region pool */
static struct address_pool utcb_region_pool;
int utcb_pool_init(unsigned long utcb_start, unsigned long utcb_end)
{
int err;
/* Initialise the global utcb virtual address pool */
if ((err = address_pool_init(&utcb_region_pool,
utcb_start, utcb_end,
UTCB_SIZE) < 0)) {
printf("UTCB address pool initialisation failed.\n");
return err;
}
return 0;
}
static inline void *utcb_new_address(int npages)
{
return address_new(&utcb_region_pool, npages);
}
static inline int utcb_delete_address(void *utcb_address, int npages)
{
return address_del(&utcb_region_pool, utcb_address, npages);
}
/* Return an empty utcb slot in this descriptor */
unsigned long utcb_new_slot(struct utcb_desc *desc)
{
int slot;
if ((slot = id_new(desc->slots)) < 0)
return 0;
else
return desc->utcb_base + (unsigned long)slot * UTCB_SIZE;
}
int utcb_delete_slot(struct utcb_desc *desc, unsigned long address)
{
BUG_ON(id_del(desc->slots, (address - desc->utcb_base)
/ UTCB_SIZE) < 0);
return 0;
}
struct utcb_desc *utcb_new_desc(void)
{
struct utcb_desc *d;
/* Allocate a new descriptor */
if (!(d = kzalloc(sizeof(*d))))
return 0;
link_init(&d->list);
/* We currently assume UTCB is smaller than PAGE_SIZE */
BUG_ON(UTCB_SIZE > PAGE_SIZE);
/* Initialise utcb slots */
d->slots = id_pool_new_init(PAGE_SIZE / UTCB_SIZE);
/* Obtain a new and unique utcb base */
/* FIXME: Use variable size than a page */
d->utcb_base = (unsigned long)utcb_new_address(1);
return d;
}
int utcb_delete_desc(struct utcb_desc *desc)
{
/* Return descriptor address */
utcb_delete_address((void *)desc->utcb_base, 1);
/* Free the descriptor */
kfree(desc);
return 0;
}

View File

@@ -1,168 +0,0 @@
#include <stdio.h>
#include <l4lib/arch/syscalls.h>
#include <l4lib/exregs.h>
#include <errno.h>
#include <malloc/malloc.h>
#include <utcb.h>
static unsigned long utcb_end_max_addr;
#if defined(MAPPING_ENABLE)
static int setup_utcb_table(unsigned long utcb_start_addr,
unsigned long utcb_end_addr)
{
unsigned long utcb_range_size;
unsigned long utcb_table_size;
int total_entry;
/*
* Establish the utcb_table.
* 1. Calculate the size of the table.
* 2. Calculate the number of entries.
* 3. Allocate it.
*/
utcb_range_size = utcb_end_addr - utcb_start_addr;
total_entry = utcb_range_size / UTCB_SIZE;
utcb_table_size = sizeof(struct utcb_entry) * total_entry;
if (!(utcb_table_ptr = kzalloc(utcb_table_size))) {
printf("libl4thread: There is not enough memory for the utcb "
"mapping table of size(%d)!\n", utcb_table_size);
return -ENOMEM;
}
return 0;
}
#endif
static int set_utcb_addr(void)
{
struct exregs_data exregs;
unsigned long utcb_addr;
struct task_ids ids;
int err;
l4_getid(&ids);
// FIXME: its tid must be 0.
#if !defined(MAPPING_ENABLE)
udesc_ptr = utcb_new_desc();
utcb_addr = get_utcb_addr();
#else
{
struct utcb_desc *udesc;
udesc = utcb_new_desc();
utcb_table_ptr[ids.tid].udesc = udesc;
utcb_addr = get_utcb_addr(&ids, &ids);
}
#endif
memset(&exregs, 0, sizeof(exregs));
exregs_set_utcb(&exregs, (unsigned long)utcb_addr);
if ((err = l4_exchange_registers(&exregs, ids.tid)) < 0) {
printf("libl4thread: l4_exchange_registers failed with "
"(%d)!\n", err);
return err;
}
return 0;
}
int set_utcb_params(unsigned long utcb_start_addr,
unsigned long utcb_end_addr)
{
int err;
/* Ensure that arguments are valid. */
if (IS_UTCB_SETUP()) {
printf("libl4thread: You have already called: %s. Simply, "
"this will have no effect!\n", __FUNCTION__);
return -EPERM;
}
if (!utcb_start_addr || !utcb_end_addr) {
printf("libl4thread: utcb address range cannot contain "
"0x00000000 as a start and/or end address(es)!\n");
return -EINVAL;
}
/* Check if the start address is aligned on UTCB_SIZE. */
if (utcb_start_addr & !UTCB_SIZE) {
printf("libl4thread: utcb start address must be aligned "
"on UTCB_SIZE(0x%x)\n", UTCB_SIZE);
return -EINVAL;
}
/* The range must be a valid one. */
if (utcb_start_addr >= utcb_end_addr) {
printf("libl4thread: utcb end address must be bigger "
"than utcb start address!\n");
return -EINVAL;
}
/*
* This check guarantees two things:
* 1. The range must be multiple of UTCB_SIZE, at least one item.
* 2. utcb_end_addr is aligned on UTCB_SIZE
*/
if ((utcb_end_addr - utcb_start_addr) % UTCB_SIZE) {
printf("libl4thread: the given range size must be multiple "
"of the utcb size(%d)!\n", UTCB_SIZE);
return -EINVAL;
}
/* Arguments passed the validity tests. */
/* Init utcb virtual address pool */
utcb_pool_init(utcb_start_addr, utcb_end_addr);
#if defined(MAPPING_ENABLE)
setup_utcb_table(utcb_start_addr, utcb_end_addr);
#endif
utcb_end_max_addr = utcb_end_addr;
/* The very first thread's utcb address is assigned. */
if ((err = set_utcb_addr()) < 0)
return err;
return 0;
}
#if !defined(MAPPING_ENABLE)
unsigned long get_utcb_addr(void)
{
unsigned long utcb_addr;
if (!(utcb_addr = utcb_new_slot(udesc_ptr))) {
udesc_ptr = utcb_new_desc();
utcb_addr = utcb_new_slot(udesc_ptr);
}
if (utcb_addr >= utcb_end_max_addr)
return 0;
return utcb_addr;
}
#else
unsigned long get_utcb_addr(struct task_ids *parent_id,
struct task_ids *child_id)
{
struct utcb_entry uentry;
unsigned long utcb_addr;
/* Get the entry for this thread. */
uentry = utcb_table_ptr[parent_id->tid];
/* Get a new utcb virtual address. */
if (!(utcb_addr = utcb_new_slot(uentry.udesc))) {
/* No slot for this desc, get a new desc. */
struct utcb_desc *udesc;
udesc = utcb_new_desc();
utcb_addr = utcb_new_slot(udesc);
uentry.udesc = udesc;
}
/* Place the entry into the utcb_table. */
utcb_table_ptr[child_id->tid].udesc = uentry.udesc;
utcb_table_ptr[parent_id->tid].udesc = uentry.udesc;
if (utcb_addr >= utcb_end_max_addr)
return 0;
return utcb_addr;
}
#endif
/*void destroy_utcb(void) {}*/