From 52587141f40523702d4ab4bae6644a862eb59688 Mon Sep 17 00:00:00 2001 From: Bora Sahin Date: Thu, 5 Nov 2009 15:20:54 +0200 Subject: [PATCH] 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 question has to be already mapped-in. There are also some minor fixes for the stack support and the utcb common helper routines. --- conts/libl4thread/include/l4thread/addr.h | 5 +- conts/libl4thread/include/l4thread/thread.h | 14 +- .../include/l4thread/utcb-common.h | 25 +++ conts/libl4thread/include/l4thread/utcb.h | 22 +-- conts/libl4thread/src/addr.c | 6 +- conts/libl4thread/src/thread.c | 32 +++- conts/libl4thread/src/utcb-common.c | 89 +++++++++++ conts/libl4thread/src/utcb.c | 148 ++++++++++-------- 8 files changed, 249 insertions(+), 92 deletions(-) create mode 100644 conts/libl4thread/include/l4thread/utcb-common.h create mode 100644 conts/libl4thread/src/utcb-common.c diff --git a/conts/libl4thread/include/l4thread/addr.h b/conts/libl4thread/include/l4thread/addr.h index 5bbc69d..e7b4519 100644 --- a/conts/libl4thread/include/l4thread/addr.h +++ b/conts/libl4thread/include/l4thread/addr.h @@ -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); diff --git a/conts/libl4thread/include/l4thread/thread.h b/conts/libl4thread/include/l4thread/thread.h index 658d17b..ce41f16 100644 --- a/conts/libl4thread/include/l4thread/thread.h +++ b/conts/libl4thread/include/l4thread/thread.h @@ -6,13 +6,21 @@ #ifndef __LIB_THREAD_H__ #define __LIB_THREAD_H__ -#define STACK_TOP_ADDR(stack) ((unsigned long)(stack)) -#define STACK_BOTTOM_ADDR(stack) \ - ((unsigned long)((stack) + (sizeof(stack)))) +#define START_ADDR(addr) ((unsigned long)(addr)) +#define END_ADDR(addr) ((unsigned long)((addr) + (sizeof(addr)))) + +#define STACK_TOP_ADDR(stack) (START_ADDR(stack)) +#define STACK_BOTTOM_ADDR(stack) (END_ADDR(stack)) + +#define UTCB_START_ADDR(utcb) (START_ADDR(utcb)) +#define UTCB_END_ADDR(utcb) (END_ADDR(utcb)) int set_stack_params(unsigned long stack_top_addr, unsigned long stack_bottom_addr, unsigned long stack_size); +int set_utcb_params(unsigned long utcb_start_addr, + unsigned long utcb_end_addr); + int thread_create(struct task_ids *ids, unsigned int flags, int (*func)(void *), void *arg); diff --git a/conts/libl4thread/include/l4thread/utcb-common.h b/conts/libl4thread/include/l4thread/utcb-common.h new file mode 100644 index 0000000..e5f5fef --- /dev/null +++ b/conts/libl4thread/include/l4thread/utcb-common.h @@ -0,0 +1,25 @@ +/* + * UTCB handling common helper routines + * + * Copyright (C) 2009 B Labs Ltd. + */ +#ifndef __UTCB_COMMON_H__ +#define __UTCB_COMMON_H__ + +#include + +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__ */ diff --git a/conts/libl4thread/include/l4thread/utcb.h b/conts/libl4thread/include/l4thread/utcb.h index 464bd45..32049e5 100644 --- a/conts/libl4thread/include/l4thread/utcb.h +++ b/conts/libl4thread/include/l4thread/utcb.h @@ -3,23 +3,13 @@ * * Copyright (C) 2009 B Labs Ltd. */ -#ifndef __UTCB_H__ -#define __UTCB_H__ +#ifndef __LIB_UTCB_H__ +#define __LIB_UTCB_H__ -#include +#define IS_UTCB_SETUP() (udesc_ptr) -struct utcb_desc { - struct link list; - unsigned long utcb_base; - struct id_pool *slots; -}; +struct utcb_desc *udesc_ptr; -int utcb_pool_init(unsigned long utcb_start, unsigned long utcb_end); +unsigned long get_utcb_addr(void); -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_H__ */ +#endif /* __LIB_UTCB_H__ */ diff --git a/conts/libl4thread/src/addr.c b/conts/libl4thread/src/addr.c index 10206bd..90c67dd 100644 --- a/conts/libl4thread/src/addr.c +++ b/conts/libl4thread/src/addr.c @@ -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; diff --git a/conts/libl4thread/src/thread.c b/conts/libl4thread/src/thread.c index e11572c..cfaaaca 100644 --- a/conts/libl4thread/src/thread.c +++ b/conts/libl4thread/src/thread.c @@ -8,6 +8,7 @@ #include #include #include +#include /* 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 " diff --git a/conts/libl4thread/src/utcb-common.c b/conts/libl4thread/src/utcb-common.c new file mode 100644 index 0000000..38df843 --- /dev/null +++ b/conts/libl4thread/src/utcb-common.c @@ -0,0 +1,89 @@ +/* + * UTCB management in Codezero + * + * Copyright © 2009 B Labs Ltd + */ +#include +#include +#include +#include + +/* 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; +} diff --git a/conts/libl4thread/src/utcb.c b/conts/libl4thread/src/utcb.c index 9dcc187..7298bf0 100644 --- a/conts/libl4thread/src/utcb.c +++ b/conts/libl4thread/src/utcb.c @@ -1,88 +1,110 @@ /* - * UTCB management in Codezero + * UTCB handling helper routines * - * Copyright © 2009 B Labs Ltd + * Copyright (C) 2009 B Labs Ltd. */ #include -#include -#include +#include +#include +#include #include +#include +#include -/* Globally disjoint utcb virtual region pool */ -static struct address_pool utcb_region_pool; +/* Static variable definitions */ +static unsigned long utcb_end_max_addr; -int utcb_pool_init(unsigned long utcb_start, unsigned long utcb_end) +/* Function definitions */ +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; +} + +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. + udesc_ptr = utcb_new_desc(); + utcb_addr = get_utcb_addr(); + + 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; -} + 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) -{ - struct utcb_desc *d; +/*void destroy_utcb(void) {}*/ - /* 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; -}