First commit XiUOS

This commit is contained in:
xuetest
2021-04-28 17:49:18 +08:00
commit 6001051eb7
1331 changed files with 433955 additions and 0 deletions
@@ -0,0 +1,4 @@
config POSIX_API
bool "support posix api"
default n
@@ -0,0 +1,19 @@
ifeq ($(CONFIG_POSIX_API),y)
SRC_FILES += pthread.c
ifeq ($(CONFIG_KERNEL_SEMAPHORE),y)
SRC_FILES += semaphore.c
endif
ifeq ($(CONFIG_KERNEL_MUTEX),y)
SRC_FILES += pthread_mutex.c
endif
ifeq ($(CONFIG_KERNEL_MESSAGEQUEUE),y)
SRC_FILES += mqueue.c
endif
else
SRC_FILES :=
endif
include $(KERNEL_ROOT)/compiler.mk
@@ -0,0 +1,55 @@
/*
* 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: fs.h
* @brief: the macor definition of posix fs
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#ifndef POSIX_FS_H
#define POSIX_FS_H
// #include "../../switch_api/user_api.h"
// #define open UserOpen
// #define read UserRead
// #define write UserWrite
// #define close UserClose
// #define lseek UserLseek
// #define rename UserRename
// #define unlink UserUnlink
// #define stat UserStat
// #define fstat UserFstat
// #define fsync UserFsync
// #define ftruncate UserFtruncate
// #define mkdir UserMkdir
// #define opendir UserOpendir
// #define closedir UserClosedir
// #define readdir UserReaddir
// #define rmdir UserRmdir
// #define chdir UserChdir
// #define getcwd UserGetcwd
// #define telldir UserTelldir
// #define seekdir UserSeekdir
// #define rewinddir UserRewinddir
// #define printf UserPrintf
// #ifdef FS_DFS
// #define statfs UserStatfs
// #endif
#endif
@@ -0,0 +1,57 @@
/*
* 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: msgqueue.h
* @brief: the function definition of posix msg queue
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#ifndef MQUEUE_H
#define MQUEUE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../../switch_api/user_api.h"
#include <time.h>
#define DEFAULT_MQUEUE_SIZE (10 * 1024)
#define DEFAULT_MAX_MSG_SIZE (1024)
typedef int mqd_t;
struct mq_attr {
long mq_flags; /* message queue flags */
long mq_maxmsg; /* maximum number of messages */
long mq_msgsize; /* maximum message size */
long mq_curmsgs; /* number of messages currently queued */
};
mqd_t mq_open(const char *name, int oflag, ...);
int mq_close(mqd_t mqdes);
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio);
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio);
int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat, struct mq_attr *omqstat);
int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat);
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio, const struct timespec *abs_timeout);
int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio, const struct timespec *abs_timeout);
int mq_unlink(const char *name);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
@@ -0,0 +1,67 @@
/*
* 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: pthread_arm.h
* @brief: the attribute definition of posix pthread for arm
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#ifndef PTHREAD_ARM_H
#define PTHREAD_ARM_H
#ifdef __cplusplus
extern "C" {
#endif
#include <time.h>
#include <sys/time.h>
typedef int pid_t;
typedef unsigned long int pthread_t;
struct sched_param {
int sched_priority; /* process execution scheduling priority */
size_t slice; /* time slice in SCHED_RR mode (ms) */
};
typedef struct pthread_attr {
unsigned char is_initialized; /* if the attr is initialized set to 1, otherwise set to 0 */
void *stackaddr; /* the start addr of the stack of the pthead */
size_t stacksize; /* the size of the stack of the pthead */
unsigned char contentionscope; /* the scope of contention, only PTHREAD_SCOPE_SYSTEM is supported */
unsigned char inheritsched; /* when set to PTHREAD_INHERIT_SCHED, specifies that the thread scheduling attributes
shall be inherited from the creating thread, and the scheduling attributes in this
attr argument shall be ignored */
unsigned char schedpolicy; /* the sched policy of the thread */
struct sched_param schedparam; /* the parameter of the thread scheduling */
size_t guardsize; /* guardsize is set to protect the stack, not supported */
unsigned char detachstate; /* when set to PTHREAD_CREATE_JOINABLE, thread will not end untill the creating thread end */
} pthread_attr_t;
typedef struct pthread_mutexattr {
int is_initialized;
int type;
int protocol;
int prioceiling;
int pshared;
} pthread_mutexattr_t;
typedef int pthread_mutex_t ;
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* PTHREAD_H */
@@ -0,0 +1,107 @@
/*
* 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: pthread.h
* @brief: the attribute definition of posix pthread
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#ifndef PTHREAD_H
#define PTHREAD_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../../switch_api/user_api.h"
#include <time.h>
#include <sys/time.h>
#if defined(ARCH_ARM)
#include "pthread arm.h"
#endif
// enum {
// PTHREAD_BARRIER_SERIAL_THREAD,
// PTHREAD_CANCEL_ASYNCHRONOUS,
// PTHREAD_CANCEL_ENABLE,
// PTHREAD_CANCEL_DEFERRED,
// PTHREAD_CANCEL_DISABLE,
// PTHREAD_CANCELED,
// PTHREAD_CREATE_DETACHED,
// PTHREAD_CREATE_JOINABLE,
// PTHREAD_EXPLICIT_SCHED,
// PTHREAD_INHERIT_SCHED,
// PTHREAD_MUTEX_DEFAULT,
// PTHREAD_MUTEX_ERRORCHECK,
// PTHREAD_MUTEX_NORMAL,
// PTHREAD_MUTEX_RECURSIVE,
// PTHREAD_MUTEX_ROBUST,
// PTHREAD_MUTEX_STALLED,
// PTHREAD_ONCE_INIT,
// PTHREAD_PRIO_INHERIT,
// PTHREAD_PRIO_NONE,
// PTHREAD_PRIO_PROTECT,
// PTHREAD_PROCESS_SHARED,
// PTHREAD_PROCESS_PRIVATE,
// PTHREAD_SCOPE_PROCESS,
// PTHREAD_SCOPE_SYSTEM,
// };
typedef int pid_t;
/* function in pthread.c */
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
void pthread_exit(void *value_ptr);
int pthread_detach(pthread_t thread);
int pthread_join(pthread_t thread, void **retval);
int pthread_cancel(pthread_t thread);
void pthread_testcancel(void);
int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
int pthread_kill(pthread_t thread, int sig);
int pthread_equal(pthread_t t1, pthread_t t2);
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *pParam);
void pthread_cleanup_pop(int execute);
void pthread_cleanup_push(void (*routine)(void *), void *arg);
pthread_t pthread_self(void);
int pthread_getcpuclockid(pthread_t thread_id, clockid_t *clock_id);
int pthread_setconcurrency(int new_level);
int pthread_getconcurrency(void);
int pthread_setschedprio(pthread_t thread, int prio);
int pthread_setname_np(pthread_t thread, const char *name);
int pthread_timedjoin_np(pthread_t thread, void **retval, const struct timespec *abstime);
/* function in pthread_mutex.c */
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
@@ -0,0 +1,49 @@
/*
* 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: semaphore.h
* @brief: the functions definition of posix semaphore
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../../switch_api/user_api.h"
#include <time.h>
typedef int sem_t;
int sem_init(sem_t *sem, int pshared, unsigned int value);
sem_t *sem_open(const char *name, int oflag, ...);
int sem_post(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
int sem_trywait(sem_t *sem);
int sem_unlink(const char *name);
int sem_wait(sem_t *sem);
int sem_getvalue(sem_t *sem, int *sval);
int sem_close(sem_t *sem);
int sem_destroy(sem_t *sem);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* SEMAPHORE_H */
@@ -0,0 +1,94 @@
/*
* 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: mqueue.c
* @brief: posix api of mqueue
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#include "include/mqueue.h"
mqd_t mq_open(const char *name, int oflag, ...)
{
mqd_t mq;
mq = UserMsgQueueCreate( DEFAULT_MQUEUE_SIZE, DEFAULT_MAX_MSG_SIZE);
if (mq < 0) {
return -1;
}
return mq;
}
int mq_close(mqd_t mqdes)
{
return UserMsgQueueDelete(mqdes);
}
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
{
ssize_t ret;
*msg_prio = 0;
ret = UserMsgQueueRecv(mqdes, msg_ptr, (unsigned long)&msg_len, 0);
return ret;
}
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
{
int ret;
ret = UserMsgQueueSend(mqdes, (void *)msg_ptr, msg_len);
return ret;
}
int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat, struct mq_attr *omqstat)
{
return 0;
}
int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat)
{
return 0;
}
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
unsigned *msg_prio, const struct timespec *abstime)
{
ssize_t ret;
int ticks;
*msg_prio = 0;
ticks = abstime->tv_sec * 1000 +
(abstime->tv_nsec / 1000000 ) ;
ret = UserMsgQueueRecv(mqdes, msg_ptr, msg_len, ticks);
return ret;
}
int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned msg_prio, const struct timespec *abstime)
{
int ret;
int ticks;
ticks = abstime->tv_sec * 1000 +
(abstime->tv_nsec / 1000000 ) ;
ret = UserMsgQueueSendwait(mqdes, (void *)msg_ptr, msg_len, ticks);
return ret;
}
@@ -0,0 +1,94 @@
/*
* 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: pthread.c
* @brief: posix api of pthread
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#include "include/pthread.h"
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
int ret ;
int pid ;
utask_x task ;
task.func_entry = start_routine ;
task.func_param = arg ;
memcpy(task.name , "utask", 6);
task.prio = 20 ;
task.stack_size = 1024 ;
pid = UserTaskCreate(task);
if (pid < 0)
return -1 ;
ret = UserTaskStartup(pid);
return ret;
}
void pthread_exit(void *value_ptr){
//todo add exit value
UserTaskQuit();
}
pthread_t pthread_self(void){
pthread_t pthread ;
pthread = UserGetTaskID();
return pthread;
}
int pthread_setschedprio(pthread_t thread, int prio)
{
//add syscall
return 0;
}
int pthread_equal(pthread_t t1, pthread_t t2)
{
return (int)(t1 == t2);
}
int pthread_cancel(pthread_t thread)
{
return -1;
}
void pthread_testcancel(void)
{
return;
}
int pthread_setcancelstate(int state, int *oldstate)
{
return -1;
}
int pthread_setcanceltype(int type, int *oldtype)
{
return -1;
}
int pthread_kill(pthread_t thread, int sig)
{
/* This api should not be used, and will not be supported */
return -1;
}
@@ -0,0 +1,125 @@
/*
* 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: pthread_mutex.c
* @brief: posix api of mutex
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#include <time.h>
#include "include/pthread.h"
int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *attr)
{
*p_mutex = UserMutexCreate();
if (*p_mutex < 0) {
return -1;
}
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *p_mutex)
{
UserMutexDelete(*p_mutex);
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *p_mutex)
{
int ret = -1;
ret = UserMutexObtain(*p_mutex, WAITING_FOREVER);
return ret;
}
int pthread_mutex_unlock(pthread_mutex_t *p_mutex)
{
int ret = -1;
ret = UserMutexAbandon( *p_mutex );
return ret;
}
int pthread_mutex_trylock(pthread_mutex_t *p_mutex)
{
int ret = -1;
ret = UserMutexObtain( *p_mutex , 0);
return ret;
}
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
return 0;
}
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
{
return 0;
}
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
{
return 0;
}
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
{
return 0;
}
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict attr, int *restrict protocol)
{
return 0;
}
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
{
return 0;
}
int pthread_mutex_getprioceiling(const pthread_mutex_t *restrict mutex, int *restrict prioceiling)
{
return 0;
}
int pthread_mutex_setprioceiling(pthread_mutex_t *restrict mutex, int prioceiling, int *restrict old_ceiling)
{
return 0;
}
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling)
{
return 0;
}
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
{
return 0;
}
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
{
return 0;
}
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
{
return 0;
}
@@ -0,0 +1,97 @@
/*
* 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: semaphore.c
* @brief: posix api of semphore
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2020/4/20
*
*/
#include "include/semaphore.h"
#include <time.h>
int sem_init(sem_t *sem, int pshared, unsigned int value)
{
int32 ret = 0;
ret = UserSemaphoreCreate(value);
if (ret < 0) {
return -1;
}
return 0;
}
sem_t *sem_open(const char *name, int oflag, ...)
{
return 0;
}
int sem_post(sem_t *sem)
{
int ret;
ret = UserSemaphoreAbandon(*sem);
return ret;
}
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
{
int ret ;
int ticks = -1 ;
if (abstime != NULL) {
ticks = abstime->tv_sec * 1000 +
(abstime->tv_nsec / 1000000 ) ;
}
ret = UserSemaphoreObtain(*sem, ticks);
return ret;
}
int sem_trywait(sem_t *sem)
{
int ret ;
ret = KSemaphoreObtain(*sem, 0);
return ret;
}
int sem_unlink(const char *name)
{
return 0;
}
int sem_wait(sem_t *sem)
{
int ret ;
ret = KSemaphoreObtain(*sem, -1);
return ret;
}
int sem_getvalue(sem_t *sem, int *sval)
{
return 0;
}
int sem_close(sem_t *sem)
{
return 0;
}
int sem_destroy(sem_t *sem)
{
UserSemaphoreDelete(*sem);
return 0;
}