2014-08-26 22:57:05 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/group/group_addrenv.c
|
2014-08-26 22:57:05 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you 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
|
2014-08-26 22:57:05 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-08-26 22:57:05 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* 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.
|
2014-08-26 22:57:05 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2014-08-26 22:57:05 +02:00
|
|
|
#include <nuttx/sched.h>
|
|
|
|
|
2014-08-28 16:41:57 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-26 22:57:05 +02:00
|
|
|
#include "group/group.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
2019-11-09 15:15:12 +01:00
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
/* This variable holds the PID of the current task group. This ID is
|
2014-08-26 22:57:05 +02:00
|
|
|
* zero if the current task is a kernel thread that has no address
|
|
|
|
* environment (other than the kernel context).
|
|
|
|
*
|
|
|
|
* This must only be accessed with interrupts disabled.
|
|
|
|
*/
|
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
pid_t g_pid_current = INVALID_PROCESS_ID;
|
2014-08-26 22:57:05 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: group_addrenv
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Instantiate the group address environment for the current thread at the
|
|
|
|
* the head of the ready to run list.
|
|
|
|
*
|
|
|
|
* This function is called from platform-specific code after any context
|
2021-02-08 18:46:31 +01:00
|
|
|
* switch (i.e., after any change in the thread at the head of the
|
|
|
|
* ready-to-run list). This function will change the address environment
|
|
|
|
* if the new thread is part of a different task group.
|
2014-08-26 22:57:05 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-08-26 22:57:05 +02:00
|
|
|
* tcb - The TCB of thread that needs an address environment. This should
|
|
|
|
* be the TCB at the head of the ready-to-run list, but that is not
|
|
|
|
* enough.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-08-26 22:57:05 +02:00
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function should only be called within critical OS sections with
|
|
|
|
* interrupts disabled. Interrupts are disabled internally just to be
|
|
|
|
* certain, however.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int group_addrenv(FAR struct tcb_s *tcb)
|
|
|
|
{
|
|
|
|
FAR struct task_group_s *group;
|
|
|
|
FAR struct task_group_s *oldgroup;
|
|
|
|
irqstate_t flags;
|
2021-03-03 18:11:40 +01:00
|
|
|
pid_t pid;
|
2014-08-26 22:57:05 +02:00
|
|
|
int ret;
|
|
|
|
|
2014-08-28 16:41:57 +02:00
|
|
|
/* NULL for the tcb means to use the TCB of the task at the head of the
|
|
|
|
* ready to run list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!tcb)
|
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
tcb = this_task();
|
2014-08-28 16:41:57 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 22:57:05 +02:00
|
|
|
DEBUGASSERT(tcb && tcb->group);
|
|
|
|
group = tcb->group;
|
|
|
|
|
2014-08-27 17:37:28 +02:00
|
|
|
/* Does the group have an address environment? */
|
2014-08-26 22:57:05 +02:00
|
|
|
|
2014-08-27 17:37:28 +02:00
|
|
|
if ((group->tg_flags & GROUP_FLAG_ADDRENV) == 0)
|
2014-08-26 22:57:05 +02:00
|
|
|
{
|
2014-08-27 17:37:28 +02:00
|
|
|
/* No... just return perhaps leaving a different address environment
|
|
|
|
* intact.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
2014-08-26 22:57:05 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
/* Get the PID of the group that needs the address environment */
|
2014-08-27 17:37:28 +02:00
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
pid = group->tg_pid;
|
|
|
|
DEBUGASSERT(pid != INVALID_PROCESS_ID);
|
2014-08-27 17:37:28 +02:00
|
|
|
|
2014-08-26 22:57:05 +02:00
|
|
|
/* Are we going to change address environments? */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2021-03-03 18:11:40 +01:00
|
|
|
if (pid != g_pid_current)
|
2014-08-26 22:57:05 +02:00
|
|
|
{
|
|
|
|
/* Yes.. Is there a current address environment in place? */
|
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
if (g_pid_current != INVALID_PROCESS_ID)
|
2014-08-26 22:57:05 +02:00
|
|
|
{
|
2014-08-27 17:37:28 +02:00
|
|
|
/* Find the old group with this ID. */
|
2014-08-26 22:57:05 +02:00
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
oldgroup = group_findbypid(g_pid_current);
|
2014-08-27 17:37:28 +02:00
|
|
|
DEBUGASSERT(oldgroup &&
|
2017-08-15 01:19:27 +02:00
|
|
|
(oldgroup->tg_flags & GROUP_FLAG_ADDRENV) != 0);
|
2014-08-26 22:57:05 +02:00
|
|
|
|
|
|
|
if (oldgroup)
|
|
|
|
{
|
|
|
|
/* We need to flush the D-Cache and Invalidate the I-Cache for
|
|
|
|
* the group whose environment is disappearing.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 00:01:44 +02:00
|
|
|
up_addrenv_coherent(&oldgroup->tg_addrenv);
|
2014-08-26 22:57:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Instantiate the new address environment (removing the old
|
|
|
|
* environment in the process). For the case of kernel threads,
|
|
|
|
* the old mappings will be removed and no new mappings will be
|
|
|
|
* instantiated.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 00:01:44 +02:00
|
|
|
ret = up_addrenv_select(&group->tg_addrenv, NULL);
|
2014-08-26 22:57:05 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_select failed: %d\n", ret);
|
2014-08-26 22:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new, current group */
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
g_pid_current = pid;
|
2014-08-26 22:57:05 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2014-08-26 22:57:05 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_ARCH_ADDRENV */
|