add capability interface into kernel
This commit is contained in:
parent
49b20dfa6a
commit
7897a91a8a
|
@ -78,6 +78,10 @@ menu "Kernel feature"
|
|||
help
|
||||
Enable task isolation
|
||||
|
||||
config KERNEL_CAPABILITY
|
||||
bool "Enable task capability"
|
||||
default n
|
||||
|
||||
menu "Inter-Task communication"
|
||||
config KERNEL_SEMAPHORE
|
||||
bool "Enable semaphore"
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: xs_capability.h
|
||||
* @brief: capability header file for XiZi-IIoT
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2024/10/14
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef XS_CAPABILITY_H
|
||||
#define XS_CAPABILITY_H
|
||||
|
||||
/* import board special configuration */
|
||||
#include <xsconfig.h>
|
||||
#include <xs_base.h>
|
||||
#include <xs_memory.h>
|
||||
#include <xs_assign.h>
|
||||
|
||||
|
||||
// assume the number of capability types is limited to 8*MAX_NUM_TA
|
||||
typedef enum {
|
||||
XS_CAP = 0,
|
||||
XS_CAP_TASK,
|
||||
XS_CAP_MEM,
|
||||
XS_CAP_RESOURCES,
|
||||
XS_CAP_MAX // the number of capability types
|
||||
} xs_capability_type;
|
||||
|
||||
typedef struct {
|
||||
uint8 flags[XS_CAP_MAX/8+1];
|
||||
} xs_capability, *xs_capability_t;
|
||||
|
||||
typedef struct {
|
||||
int32 pid;
|
||||
xs_capability cap;
|
||||
} xs_task_capability, *xs_task_capability_t;
|
||||
|
||||
typedef struct capability_node{
|
||||
xs_task_capability inner;
|
||||
struct capability_node* next;
|
||||
} capability_node, *capability_node_t;
|
||||
|
||||
|
||||
// functions to manipulate capability flags
|
||||
int CheckCapability(xs_capability *cap, xs_capability_type type);
|
||||
|
||||
// set_capability and clear_capability are used to manipulate capability flags
|
||||
void SetCapability(xs_capability *cap, xs_capability_type type);
|
||||
|
||||
// clear_capability is used to manipulate capability flags
|
||||
void ClearCapability(xs_capability *cap, xs_capability_type type);
|
||||
|
||||
// functions to manipulate task capabilities
|
||||
xs_capability* FindTaskCapability(int32 pid);
|
||||
|
||||
// add_task_capability and remove_task_capability are used to manipulate task capabilities
|
||||
void AddTaskCapability(int32 pid, xs_capability *cap);
|
||||
|
||||
// remove_task_capability is used to manipulate task capabilities
|
||||
void RemoveTaskCapability(int32 pid);
|
||||
|
||||
// check_task_capability is used to check if a task has a certain capability
|
||||
x_bool CheckTaskCapability(int32 pid, xs_capability_type type);
|
||||
|
||||
// set_task_capability and clear_task_capability are used to manipulate task capabilities
|
||||
void SetTaskCapability(int32 pid, xs_capability_type type);
|
||||
|
||||
// clear_task_capability is used to manipulate task capabilities
|
||||
void ClearTaskCapability(int32 pid, xs_capability_type type);
|
||||
|
||||
#endif /* XS_CAPABILITY_H */
|
||||
|
||||
|
|
@ -11,6 +11,10 @@ menuconfig KERNEL_TEST
|
|||
bool "Config test Circular area"
|
||||
default n
|
||||
select KERNEL_CIRCULAR_AREA
|
||||
config KERNEL_TEST_CAPABILITY
|
||||
bool "Config test capability"
|
||||
default n
|
||||
select KERNEL_AVL_TREE
|
||||
config KERNEL_TEST_MEM
|
||||
bool "Config test mem"
|
||||
default n
|
||||
|
|
|
@ -16,6 +16,11 @@ ifeq ($(CONFIG_KERNEL_TEST_MSG),y)
|
|||
SRC_FILES += test_mq.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_KERNEL_TEST_CAPABILITY),y)
|
||||
SRC_FILES += test_capability.c
|
||||
endif
|
||||
|
||||
|
||||
ifeq ($(CONFIG_KERNEL_TEST_AVLTREE),y)
|
||||
SRC_FILES += test_avltree.c
|
||||
endif
|
||||
|
|
|
@ -75,4 +75,9 @@ ifeq ($(CONFIG_USER_APPLICATION),y)
|
|||
SRC_FILES += appstartup.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_KERNEL_CAPABILITY),y)
|
||||
SRC_FILES += capability.c
|
||||
endif
|
||||
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
#include "xs_capability.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define NULL 0L
|
||||
#else
|
||||
#define NULL ((void*)0)
|
||||
#endif
|
||||
|
||||
// global task capability list, used to manage task capabilities
|
||||
static capability_node task_capabilities={
|
||||
.next = NULL
|
||||
};
|
||||
|
||||
// functions to manipulate capability flags
|
||||
int CheckCapability(xs_capability *cap, xs_capability_type type){
|
||||
return cap->flags[type/8] & (1 << (type%8));
|
||||
}
|
||||
// set_capability and clear_capability are used to manipulate capability flags
|
||||
void SetCapability(xs_capability *cap, xs_capability_type type){
|
||||
cap->flags[type/8] |= (1 << (type%8));
|
||||
}
|
||||
|
||||
// clear_capability is used to manipulate capability flags
|
||||
void ClearCapability(xs_capability *cap, xs_capability_type type){
|
||||
cap->flags[type/8] &= ~(1 << (type%8));
|
||||
}
|
||||
|
||||
// functions to manipulate task capabilities
|
||||
xs_capability* FindTaskCapability(int32 pid){
|
||||
capability_node* node = task_capabilities.next;
|
||||
while(node){
|
||||
if(node->inner.pid == pid){
|
||||
return &node->inner.cap;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// add_task_capability and remove_task_capability are used to manipulate task capabilities
|
||||
void AddTaskCapability(int32 pid, xs_capability *cap){
|
||||
capability_node* node = (capability_node*)x_malloc(sizeof(capability_node));
|
||||
node->inner.pid = pid;
|
||||
node->inner.cap = *cap;
|
||||
node->next = task_capabilities.next;
|
||||
task_capabilities.next = node;
|
||||
}
|
||||
|
||||
// remove_task_capability is used to manipulate task capabilities
|
||||
void RemoveTaskCapability(int32 pid){
|
||||
capability_node* node = &task_capabilities;
|
||||
while(node->next){
|
||||
if(node->next->inner.pid == pid){
|
||||
capability_node* removing = node->next;
|
||||
node->next = node->next->next;
|
||||
x_free(removing);
|
||||
return;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
|
||||
// check_task_capability is used to check if a task has a certain capability
|
||||
x_bool CheckTaskCapability(int32 pid, xs_capability_type type){
|
||||
x_base lock = CriticalAreaLock();
|
||||
xs_capability* cap = FindTaskCapability(pid);
|
||||
x_bool result = cap && CheckCapability(cap, type);
|
||||
CriticalAreaUnLock(lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
// set_task_capability and clear_task_capability are used to manipulate task capabilities
|
||||
void SetTaskCapability(int32 pid, xs_capability_type type){
|
||||
x_base lock = CriticalAreaLock();
|
||||
xs_capability* cap = FindTaskCapability(pid);
|
||||
if(cap){
|
||||
SetCapability(cap, type);
|
||||
}
|
||||
CriticalAreaUnLock(lock);
|
||||
}
|
||||
|
||||
// clear_task_capability is used to manipulate task capabilities
|
||||
void ClearTaskCapability(int32 pid, xs_capability_type type){
|
||||
x_base lock = CriticalAreaLock();
|
||||
xs_capability* cap = FindTaskCapability(pid);
|
||||
if(cap){
|
||||
ClearCapability(cap, type);
|
||||
}
|
||||
CriticalAreaUnLock(lock);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue