2015-06-19 22:32:52 +08:00
|
|
|
/* k_server.c - microkernel server */
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, 2012-2015 Wind River Systems, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2) Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3) Neither the name of Wind River Systems nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
DESCRIPTION
|
2015-05-07 05:23:01 +08:00
|
|
|
This module implements the microkernel server, which processes service requests
|
|
|
|
from tasks (and, less commonly, fibers and ISRs). The requests are service by
|
|
|
|
a high priority fiber, thereby ensuring that requests are processed in a timely
|
|
|
|
manner and in a single threaded manner that prevents simultaneous requests from
|
|
|
|
interfering with each other.
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
#include <toolchain.h>
|
|
|
|
#include <sections.h>
|
2015-06-19 22:56:52 +08:00
|
|
|
#include <micro_private.h>
|
2015-06-19 23:07:02 +08:00
|
|
|
#include <nano_private.h>
|
2015-04-11 07:44:37 +08:00
|
|
|
#include <microkernel.h>
|
|
|
|
#include <nanokernel.h>
|
|
|
|
#include <misc/__assert.h>
|
|
|
|
#include <drivers/system_timer.h>
|
|
|
|
|
2015-04-27 23:28:16 +08:00
|
|
|
extern const kernelfunc _k_server_dispatch_table[];
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-07-02 05:22:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2015-07-02 05:51:40 +08:00
|
|
|
* @brief Select task to be executed by microkernel
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
|
|
|
* Locates that highest priority task queue that is non-empty and chooses the
|
|
|
|
* task at the head of that queue. It's guaranteed that there will always be
|
|
|
|
* a non-empty queue, since the idle task is always executable.
|
|
|
|
*
|
2015-07-02 05:29:04 +08:00
|
|
|
* @return pointer to selected task
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-25 04:34:53 +08:00
|
|
|
|
|
|
|
static struct k_proc *next_task_select(void)
|
|
|
|
{
|
|
|
|
int K_PrioListIdx;
|
|
|
|
|
|
|
|
#if (CONFIG_NUM_TASK_PRIORITIES <= 32)
|
|
|
|
K_PrioListIdx = find_first_set_inline(_k_task_priority_bitmap[0]) - 1;
|
|
|
|
#else
|
|
|
|
int bit_map;
|
|
|
|
int set_bit_pos;
|
|
|
|
|
|
|
|
K_PrioListIdx = -1;
|
|
|
|
for (bit_map = 0; ; bit_map++) {
|
|
|
|
set_bit_pos = find_first_set_inline(_k_task_priority_bitmap[bit_map]);
|
|
|
|
if (set_bit_pos) {
|
|
|
|
K_PrioListIdx += set_bit_pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
K_PrioListIdx += 32;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return _k_task_priority_list[K_PrioListIdx].Head;
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:22:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2015-07-02 05:51:40 +08:00
|
|
|
* @brief The microkernel thread entry point
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
|
|
|
* This function implements the microkernel fiber. It waits for command
|
|
|
|
* packets to arrive on its stack channel. It executes all commands on the
|
|
|
|
* stack and then sets up the next task that is ready to run. Next it
|
|
|
|
* goes to wait on further inputs on its stack channel.
|
|
|
|
*
|
2015-07-02 05:29:04 +08:00
|
|
|
* @return Does not return.
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
FUNC_NORETURN void K_swapper(int parameter1, /* not used */
|
|
|
|
int parameter2 /* not used */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct k_args *pArgs;
|
|
|
|
struct k_proc *pNextTask;
|
|
|
|
|
|
|
|
ARG_UNUSED(parameter1);
|
|
|
|
ARG_UNUSED(parameter2);
|
|
|
|
|
|
|
|
/* indicate that failure of this fiber may be fatal to the entire system
|
|
|
|
*/
|
|
|
|
|
2015-05-09 06:12:45 +08:00
|
|
|
_nanokernel.current->flags |= ESSENTIAL;
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
while (1) { /* forever */
|
|
|
|
pArgs = (struct k_args *)nano_fiber_stack_pop_wait(
|
2015-04-27 23:28:29 +08:00
|
|
|
&_k_command_stack); /* will schedule */
|
2015-04-11 07:44:37 +08:00
|
|
|
do {
|
|
|
|
kevent_t event;
|
2015-04-27 23:28:30 +08:00
|
|
|
/* if event < _k_num_events, it's a well-known event */
|
2015-04-11 07:44:37 +08:00
|
|
|
event = (kevent_t)(pArgs);
|
2015-04-27 23:28:30 +08:00
|
|
|
if (event < (kevent_t)_k_num_events) {
|
2015-04-11 07:44:37 +08:00
|
|
|
#ifdef CONFIG_TASK_MONITOR
|
2015-04-27 23:28:38 +08:00
|
|
|
if (_k_monitor_mask & MON_EVENT) {
|
2015-04-29 02:35:51 +08:00
|
|
|
_k_task_monitor_args(pArgs);
|
2015-04-11 07:44:37 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-29 02:35:51 +08:00
|
|
|
_k_do_event_signal(event);
|
2015-04-11 07:44:37 +08:00
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_TASK_MONITOR
|
2015-04-27 23:28:38 +08:00
|
|
|
if (_k_monitor_mask & MON_KSERV) {
|
2015-04-29 02:35:51 +08:00
|
|
|
_k_task_monitor_args(pArgs);
|
2015-04-11 07:44:37 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-27 23:28:16 +08:00
|
|
|
_k_server_dispatch_table[pArgs->Comm](pArgs);
|
2015-04-11 07:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if another fiber (of equal or greater priority)
|
|
|
|
* needs to run */
|
|
|
|
|
2015-05-09 06:12:45 +08:00
|
|
|
if (_nanokernel.fiber) {
|
2015-04-11 07:44:37 +08:00
|
|
|
fiber_yield();
|
|
|
|
}
|
2015-04-27 23:28:29 +08:00
|
|
|
} while (nano_fiber_stack_pop(&_k_command_stack, (void *)&pArgs));
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-04-25 04:34:53 +08:00
|
|
|
pNextTask = next_task_select();
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-04-27 23:28:27 +08:00
|
|
|
if (_k_current_task != pNextTask) {
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-05-08 22:08:12 +08:00
|
|
|
/* switch from currently selected task to a different one */
|
|
|
|
|
|
|
|
#ifdef CONFIG_WORKLOAD_MONITOR
|
2015-04-11 07:44:37 +08:00
|
|
|
if (pNextTask->Ident == 0x00000000) {
|
2015-05-08 22:08:12 +08:00
|
|
|
_k_workload_monitor_idle_start();
|
|
|
|
} else if (_k_current_task->Ident == 0x00000000) {
|
|
|
|
_k_workload_monitor_idle_end();
|
2015-04-11 07:44:37 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-08 22:08:12 +08:00
|
|
|
|
2015-04-27 23:28:27 +08:00
|
|
|
_k_current_task = pNextTask;
|
2015-05-09 06:12:45 +08:00
|
|
|
_nanokernel.task = (tCCS *)pNextTask->workspace;
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_TASK_MONITOR
|
2015-04-27 23:28:38 +08:00
|
|
|
if (_k_monitor_mask & MON_TSWAP) {
|
2015-04-29 02:35:51 +08:00
|
|
|
_k_task_monitor(_k_current_task, 0);
|
2015-04-11 07:44:37 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Code analyzers may complain that K_swapper() uses an infinite loop
|
|
|
|
* unless we indicate that this is intentional
|
|
|
|
*/
|
|
|
|
|
|
|
|
CODE_UNREACHABLE;
|
|
|
|
}
|