The second step in creating a thread library.

Utcb support has beed added. It has the same drawback as in the stack support:
the area in questen has to be already mapped-in. There is some basic
infrastructure for utcb to support mapping but it is far from being complete.
MAPPING_ENABLE symbolic constant controls this behaviour.

There are also some minor fixes for the stack support and the utcb common helper
routines.
This commit is contained in:
Bora Sahin
2009-11-04 16:16:19 +02:00
parent 352cb2daaa
commit 55af5c83cb
8 changed files with 316 additions and 81 deletions

View File

@@ -18,8 +18,9 @@ struct address_pool {
int address_pool_init_with_idpool(struct address_pool *pool,
struct id_pool *idpool,
unsigned long start, unsigned long end);
int address_pool_init(struct address_pool *pool, unsigned long start,
unsigned long end);
int address_pool_init(struct address_pool *pool,
unsigned long start, unsigned long end,
int size);
void *address_new(struct address_pool *pool, int npages);
int address_del(struct address_pool *, void *addr, int npages);

View File

@@ -6,6 +6,8 @@
#ifndef __LIB_THREAD_H__
#define __LIB_THREAD_H__
#include "utcb.h"
#define STACK_TOP_ADDR(stack) ((unsigned long)(stack))
#define STACK_BOTTOM_ADDR(stack) \
((unsigned long)((stack) + (sizeof(stack))))

View File

@@ -0,0 +1,25 @@
/*
* UTCB handling common helper routines
*
* Copyright (C) 2009 B Labs Ltd.
*/
#ifndef __UTCB_COMMON_H__
#define __UTCB_COMMON_H__
#include <l4/lib/list.h>
struct utcb_desc {
struct link list;
unsigned long utcb_base;
struct id_pool *slots;
};
int utcb_pool_init(unsigned long utcb_start, unsigned long utcb_end);
unsigned long utcb_new_slot(struct utcb_desc *desc);
int utcb_delete_slot(struct utcb_desc *desc, unsigned long address);
struct utcb_desc *utcb_new_desc(void);
int utcb_delete_desc(struct utcb_desc *desc);
#endif /* __UTCB_COMMON_H__ */

View File

@@ -3,23 +3,39 @@
*
* Copyright (C) 2009 B Labs Ltd.
*/
#ifndef __UTCB_H__
#define __UTCB_H__
#ifndef __LIB_UTCB_H__
#define __LIB_UTCB_H__
#include <l4/lib/list.h>
#include "utcb-common.h"
struct utcb_desc {
struct link list;
unsigned long utcb_base;
struct id_pool *slots;
#define UTCB_START_ADDR(utcb) ((unsigned long)(utcb))
#define UTCB_END_ADDR(utcb) \
((unsigned long)((utcb) + (sizeof(utcb))))
int set_utcb_params(unsigned long utcb_start_addr,
unsigned long utcb_end_addr);
//#define MAPPING_ENABLE
#if defined(MAPPING_ENABLE)
#define IS_UTCB_SETUP() (utcb_table_ptr)
struct utcb_entry *utcb_table_ptr;
struct utcb_entry {
struct utcb_desc *udesc;
unsigned long utcb_phys_base;
};
int utcb_pool_init(unsigned long utcb_start, unsigned long utcb_end);
unsigned long get_utcb_addr(struct task_ids *parent_id, struct task_ids *child_id);
#else /* !MAPPING_ENABLE */
unsigned long utcb_new_slot(struct utcb_desc *desc);
int utcb_delete_slot(struct utcb_desc *desc, unsigned long address);
#define IS_UTCB_SETUP() (udesc_ptr)
struct utcb_desc *utcb_new_desc(void);
int utcb_delete_desc(struct utcb_desc *desc);
struct utcb_desc *udesc_ptr;
#endif /* __UTCB_H__ */
unsigned long get_utcb_addr(void);
#endif /* MAPPING_ENABLE */
#endif /* __LIB_UTCB_H__ */

View File

@@ -22,9 +22,11 @@ int address_pool_init_with_idpool(struct address_pool *pool,
return 0;
}
int address_pool_init(struct address_pool *pool, unsigned long start, unsigned long end)
int address_pool_init(struct address_pool *pool,
unsigned long start, unsigned long end,
int size)
{
if ((pool->idpool = id_pool_new_init(__pfn(end - start))) < 0)
if ((pool->idpool = id_pool_new_init((end - start) / size)) < 0)
return (int)pool->idpool;
pool->start = start;
pool->end = end;

View File

@@ -8,6 +8,7 @@
#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)
@@ -26,11 +27,18 @@ 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.
@@ -44,12 +52,15 @@ int set_stack_params(unsigned long stack_top_addr,
printf("libl4thread: Stack size cannot be zero!\n");
return -EINVAL;
}
/* stack_size at least must be equal to the difference */
/* 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!\n");
"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;
@@ -61,13 +72,15 @@ 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()) {
printf("libl4thread: Stack has not been set up. Before "
"calling thread_create, set_stack_params "
"has to be called!\n");
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;
}
@@ -85,6 +98,12 @@ int thread_create(struct task_ids *ids, unsigned int flags,
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);
@@ -99,6 +118,7 @@ int thread_create(struct task_ids *ids, unsigned int flags,
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 "

View File

@@ -0,0 +1,89 @@
/*
* 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,88 +1,168 @@
/*
* UTCB management in Codezero
*
* Copyright © 2009 B Labs Ltd
*/
#include <stdio.h>
#include <addr.h>
#include <utcb.h>
#include <l4lib/arch/syscalls.h>
#include <l4lib/exregs.h>
#include <errno.h>
#include <malloc/malloc.h>
#include <utcb.h>
/* Globally disjoint utcb virtual region pool */
static struct address_pool utcb_region_pool;
static unsigned long utcb_end_max_addr;
int utcb_pool_init(unsigned long utcb_start, unsigned long utcb_end)
#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;
/* Initialise the global utcb virtual address pool */
if ((err = address_pool_init(&utcb_region_pool,
utcb_start, utcb_end) < 0)) {
printf("UTCB address pool initialisation failed.\n");
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;
}
static inline void *utcb_new_address(int npages)
int set_utcb_params(unsigned long utcb_start_addr,
unsigned long utcb_end_addr)
{
return address_new(&utcb_region_pool, npages);
}
int err;
static inline int utcb_delete_address(void *utcb_address, int npages)
{
return address_del(&utcb_region_pool, utcb_address, npages);
}
/* 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. */
/* Return an empty utcb slot in this descriptor */
unsigned long utcb_new_slot(struct utcb_desc *desc)
{
int slot;
/* Init utcb virtual address pool */
utcb_pool_init(utcb_start_addr, utcb_end_addr);
if ((slot = id_new(desc->slots)) < 0)
return 0;
else
return desc->utcb_base + (unsigned long)slot * UTCB_SIZE;
}
#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;
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)
#if !defined(MAPPING_ENABLE)
unsigned long get_utcb_addr(void)
{
struct utcb_desc *d;
unsigned long utcb_addr;
/* Allocate a new descriptor */
if (!(d = kzalloc(sizeof(*d))))
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;
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;
return utcb_addr;
}
int utcb_delete_desc(struct utcb_desc *desc)
#else
unsigned long get_utcb_addr(struct task_ids *parent_id,
struct task_ids *child_id)
{
/* Return descriptor address */
utcb_delete_address((void *)desc->utcb_base, 1);
struct utcb_entry uentry;
unsigned long utcb_addr;
/* Free the descriptor */
kfree(desc);
/* Get the entry for this thread. */
uentry = utcb_table_ptr[parent_id->tid];
return 0;
/* 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) {}*/