zephyr/kernel/unified/mem_map.c
Benjamin Walsh 456c6daa9f unified: initial unified kernel implementation
Summary of what this includes:

    initialization:

    Copy from nano_init.c, with the following changes:

    - the main thread is the continuation of the init thread, but an idle
      thread is created as well

    - _main() initializes threads in groups and starts the EXE group

    - the ready queues are initialized

    - the main thread is marked as non-essential once the system init is
      done

    - a weak main() symbol is provided if the application does not provide a
      main() function

    scheduler:

    Not an exhaustive list, but basically provide primitives for:

    - adding/removing a thread to/from a wait queue
    - adding/removing a thread to/from the ready queue
    - marking thread as ready
    - locking/unlocking the scheduler
      - instead of locking interrupts
    - getting/setting thread priority
      - checking what state (coop/preempt) a thread is currenlty running in
    - rescheduling threads
    - finding what thread is the next to run
    - yielding/sleeping/aborting sleep
    - finding the current thread

    threads:

    - Add operationns on threads, such as creating and starting them.

    standardized handling of kernel object return codes:

    - Kernel objects now cause _Swap() to return the following values:
         0      => operation successful
        -EAGAIN => operation timed out
        -Exxxxx => operation failed for another reason

    - The thread's swap_data field can be used to return any additional
    information required to complete the operation, such as the actual
    result of a successful operation.

    timeouts:

    - same as nano timeouts, renamed to simply 'timeouts'
    - the kernel is still tick-based, but objects take timeout values in
      ms for forward compatibility with a tickless kernel.

    semaphores:

      - Port of the nanokernel semaphores, which have the same basic behaviour
      as the microkernel ones. Semaphore groups are not yet implemented.

      - These semaphores are enhanced in that they accept an initial count and a
      count limit. This allows configuring them as binary semaphores, and also
      provisioning them without having to "give" the semaphore multiple times
      before using them.

    mutexes:

    - Straight port of the microkernel mutexes. An init function is added to
    allow defining them at runtime.

    pipes:

    - straight port

    timers:

    - amalgamation of nano and micro timers, with all functionalities
      intact.

    events:

    - re-implementation, using semaphores and workqueues.

    mailboxes:

    - straight port

    message queues:

    - straight port of  microkernel FIFOs

    memory maps:

    - straight port

    workqueues:

    - Basically, have all APIs follow the k_ naming rule, and use the _timeout
    subsystem from the unified kernel directory, and not the _nano_timeout
    one.

    stacks:

    - Port of the nanokernel stacks. They can now have multiple threads
    pending on them and threads can wait with a timeout.

    LIFOs:

    - Straight port of the nanokernel LIFOs.

    FIFOs:

    - Straight port of the nanokernel FIFOs.

Work by: Dmitriy Korovkin <dmitriy.korovkin@windriver.com>
         Peter Mitsis <peter.mitsis@windriver.com>
         Allan Stephens <allan.stephens@windriver.com>
         Benjamin Walsh <benjamin.walsh@windriver.com>

Change-Id: Id3cadb3694484ab2ca467889cfb029be3cd3a7d6
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-09-13 17:12:55 -04:00

167 lines
4.1 KiB
C

/*
* Copyright (c) 2016 Wind River Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <kernel.h>
#include <nano_private.h>
#include <misc/debug/object_tracing_common.h>
#include <toolchain.h>
#include <sections.h>
#include <wait_q.h>
#include <misc/dlist.h>
#include <sched.h>
extern struct k_mem_map _k_mem_map_ptr_start[];
extern struct k_mem_map _k_mem_map_ptr_end[];
/**
* @brief Initialize kernel memory map subsystem.
*
* Perform any initialization of memory maps that wasn't done at build time.
* Currently this just involves creating the list of free blocks for each map.
*
* @return N/A
*/
static void create_free_list(struct k_mem_map *map)
{
char *p;
int j;
map->free_list = NULL;
p = map->buffer;
for (j = 0; j < map->num_blocks; j++) {
*(char **)p = map->free_list;
map->free_list = p;
p += map->block_size;
}
}
/**
* @brief Complete initialization of statically defined memory maps.
*
* Perform any initialization that wasn't done at build time.
*
* @return N/A
*/
void _k_mem_map_init(void)
{
struct k_mem_map *map;
for (map = _k_mem_map_ptr_start; map < _k_mem_map_ptr_end; map++) {
create_free_list(map);
}
}
/**
* @brief Initialize a memory map.
*
* Initializes the memory map and creates its list of free blocks.
*
* @param map Address of memory map.
* @param num_blocks Number of blocks.
* @param block_size Size of each block, in bytes.
* @param buffer Pointer to buffer used for the blocks.
*
* @return N/A
*/
void k_mem_map_init(struct k_mem_map *map, int num_blocks, int block_size,
void *buffer)
{
map->num_blocks = num_blocks;
map->block_size = block_size;
map->buffer = buffer;
map->num_used = 0;
create_free_list(map);
sys_dlist_init(&map->wait_q);
SYS_TRACING_OBJ_INIT(mem_map, map);
}
/**
* @brief Allocate a memory map block.
*
* Takes a block from the list of unused blocks.
*
* @param map Pointer to memory map object.
* @param mem Pointer to area to receive block address.
* @param timeout Maximum time (nanoseconds) to wait for allocation to complete.
* Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
* necessary.
*
* @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
*/
int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout)
{
unsigned int key = irq_lock();
int result;
if (map->free_list != NULL) {
/* take a free block */
*mem = map->free_list;
map->free_list = *(char **)(map->free_list);
map->num_used++;
result = 0;
} else if (timeout == K_NO_WAIT) {
/* don't wait for a free block to become available */
*mem = NULL;
result = -ENOMEM;
} else {
/* wait for a free block or timeout */
_pend_current_thread(&map->wait_q, timeout);
result = _Swap(key);
if (result == 0) {
*mem = _current->swap_data;
}
return result;
}
irq_unlock(key);
return result;
}
/**
* @brief Free a memory map block.
*
* Gives block to a waiting thread if there is one, otherwise returns it to
* the list of unused blocks.
*
* @param map Pointer to memory map object.
* @param mem Pointer to area to containing block address.
*
* @return N/A
*/
void k_mem_map_free(struct k_mem_map *map, void **mem)
{
int key = irq_lock();
struct tcs *pending_thread = _unpend_first_thread(&map->wait_q);
if (pending_thread) {
_set_thread_return_value_with_data(pending_thread, 0, *mem);
_timeout_abort(pending_thread);
_ready_thread(pending_thread);
if (_must_switch_threads()) {
_Swap(key);
return;
}
} else {
**(char ***)mem = map->free_list;
map->free_list = *(char **)mem;
map->num_used--;
}
irq_unlock(key);
}