Skip to content

Commit

Permalink
support compile with mlibc
Browse files Browse the repository at this point in the history
  • Loading branch information
0Bitbiscuits committed Dec 22, 2024
1 parent 2d78d8c commit ca8de25
Show file tree
Hide file tree
Showing 13 changed files with 898 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components/libc/compilers/common/include/sys/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extern "C" {
#include <signal.h>
#endif

#elif defined(RT_USING_MLIBC)
#include <signal.h>

#else

#include <stdint.h>
Expand Down
37 changes: 37 additions & 0 deletions components/libc/compilers/mlibc/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
from building import *
Import('rtconfig')

group = []

if rtconfig.PLATFORM == 'gcc':
from gcc import *
else:
Return('group')

mlibc_version = "0.0.1"

if mlibc_version and GetDepend('RT_USING_EXTERNAL_LIBC'):
print('mlibc version: ' + mlibc_version)

cwd = GetCurrentDir()
src = Glob('*.c')

CPPPATH = [cwd]
if GetDepend("RT_USING_SMART"):
CPPPATH += [cwd + '/smart']
CPPPATH += [cwd + '/smart/sys']
print(CPPPATH)
CPPDEFINES = ['RT_USING_MLIBC', 'RT_USING_LIBC'] # identify this is Newlib, and only enable POSIX.1-1990
# LIBS = ['c', 'm'] # link libc and libm
AddDepend(['RT_USING_PICOLIBC', 'RT_USING_LIBC'])

group = group + DefineGroup('Compiler', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)#, LIBS = LIBS)

list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
group = group + SConscript(os.path.join(d, 'SConscript'))

Return('group')
34 changes: 34 additions & 0 deletions components/libc/compilers/mlibc/alltypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) mlibc & plct lab
*
* SPDX-License-Identifier: MIT
*
* Change Logs:
* Date Author Notes
* 2023/06/16 bernard the first verison
*/

#ifndef MLIBC_ALLTYPES_H__
#define MLIBC_ALLTYPES_H__

#include <stdint.h>

typedef uint64_t fsblkcnt_t;
typedef uint64_t fsfilcnt_t;

#define INT_FAST16_MIN INT32_MIN
#define INT_FAST32_MIN INT32_MIN

#define INT_FAST16_MAX INT32_MAX
#define INT_FAST32_MAX INT32_MAX

#define UINT_FAST16_MAX UINT32_MAX
#define UINT_FAST32_MAX UINT32_MAX

#if (defined(__GNUC__) && (__SIZEOF_POINTER__ == 8))
#define __LONG_MAX INT64_MAX
#else
#define __LONG_MAX INT32_MAX
#endif /* __GNUC__ */

#endif
141 changes: 141 additions & 0 deletions components/libc/compilers/mlibc/smart/sched.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) mlibc & plct lab
*
* SPDX-License-Identifier: MIT
*
* Change Logs:
* Date Author Notes
* 2024/8/8 0Bitbiscuits the first version
*/
#ifndef MLIBC_SCHED_H__
#define MLIBC_SCHED_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <features.h>
#include <alltypes.h>

/* Defined a set of scheduling policies and a scheduling flag */
#define SCHED_OTHER 0
#define SCHED_FIFO 1
#define SCHED_RR 2
#define SCHED_BATCH 3
#define SCHED_IDLE 5
#define SCHED_DEADLINE 6
#define SCHED_RESET_ON_FORK 0x40000000

/* Defined a set of flags for the clone system call,
** which control how the new process (or thread)
** inherits and shares the parent process's resources.
**/
#ifdef __GNUC__
#define CSIGNAL 0x000000ff
#define CLONE_NEWTIME 0x00000080
#define CLONE_VM 0x00000100
#define CLONE_FS 0x00000200
#define CLONE_FILES 0x00000400
#define CLONE_SIGHAND 0x00000800
#define CLONE_PIDFD 0x00001000
#define CLONE_PTRACE 0x00002000
#define CLONE_VFORK 0x00004000
#define CLONE_PARENT 0x00008000
#define CLONE_THREAD 0x00010000
#define CLONE_NEWNS 0x00020000
#define CLONE_SYSVSEM 0x00040000
#define CLONE_SETTLS 0x00080000
#define CLONE_PARENT_SETTID 0x00100000
#define CLONE_CHILD_CLEARTID 0x00200000
#define CLONE_DETACHED 0x00400000
#define CLONE_UNTRACED 0x00800000
#define CLONE_CHILD_SETTID 0x01000000
#define CLONE_NEWCGROUP 0x02000000
#define CLONE_NEWUTS 0x04000000
#define CLONE_NEWIPC 0x08000000
#define CLONE_NEWUSER 0x10000000
#define CLONE_NEWPID 0x20000000
#define CLONE_NEWNET 0x40000000
#define CLONE_IO 0x80000000
#endif /* __GNUC__ */

struct sched_param {
int sched_priority;
int __reserved1;
struct {
time_t __reserved1;
long __reserved2;
} __reserved2[2];
int __reserved3;
};

int sched_get_priority_max(int);
int sched_get_priority_min(int);
int sched_getparam(pid_t, struct sched_param *);
int sched_getscheduler(pid_t);
int sched_rr_get_interval(pid_t, struct timespec *);
int sched_setparam(pid_t, const struct sched_param *);
int sched_setscheduler(pid_t, int, const struct sched_param *);
int sched_yield(void);

int clone (int (*)(void *), void *, int, void *, ...);
int unshare(int);
int setns(int, int);

typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t;
int __sched_cpucount(size_t, const cpu_set_t *);
int sched_getcpu(void);
int sched_getaffinity(pid_t, size_t, cpu_set_t *);
int sched_setaffinity(pid_t, size_t, const cpu_set_t *);

#define __CPU_op_S(i, size, set, op) ( (i)/8U >= (size) ? 0 : \
(((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )

#define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=)
#define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &=~)
#define CPU_ISSET_S(i, size, set) __CPU_op_S(i, size, set, &)

#define __CPU_op_func_S(func, op) \
static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, \
const cpu_set_t *__src1, const cpu_set_t *__src2) \
{ \
size_t __i; \
for (__i=0; __i<__size/sizeof(long); __i++) \
((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] \
op ((unsigned long *)__src2)[__i] ; \
}

__CPU_op_func_S(AND, &)
__CPU_op_func_S(OR, |)
__CPU_op_func_S(XOR, ^)

#define CPU_AND_S(a,b,c,d) __CPU_AND_S(a,b,c,d)
#define CPU_OR_S(a,b,c,d) __CPU_OR_S(a,b,c,d)
#define CPU_XOR_S(a,b,c,d) __CPU_XOR_S(a,b,c,d)

#define CPU_COUNT_S(size,set) __sched_cpucount(size,set)
#define CPU_ZERO_S(size,set) memset(set,0,size)
#define CPU_EQUAL_S(size,set1,set2) (!memcmp(set1,set2,size))

#define CPU_ALLOC_SIZE(n) (sizeof(long) * ( (n)/(8*sizeof(long)) \
+ ((n)%(8*sizeof(long)) + 8*sizeof(long)-1)/(8*sizeof(long)) ) )
#define CPU_ALLOC(n) ((cpu_set_t *)calloc(1,CPU_ALLOC_SIZE(n)))
#define CPU_FREE(set) free(set)

#define CPU_SETSIZE 1024

#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set)
#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set)
#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set)
#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2)
#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2)
#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2)
#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set)
#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set)
#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2)

#ifdef __cplusplus
}
#endif

#endif
73 changes: 73 additions & 0 deletions components/libc/compilers/mlibc/smart/sys/epoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) mlibc & plct lab
*
* SPDX-License-Identifier: MIT
*
* Change Logs:
* Date Author Notes
* 2024/8/8 0Bitbiscuits the first version
*/
#ifndef MLIBC_EPOLL_H__
#define MLIBC_EPOLL_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <sys/types.h>
#include <fcntl.h>

#include <alltypes.h>

/* The flag of file descriptor */
#define EPOLL_CLOEXEC O_CLOEXEC
#define EPOLL_NONBLOCK O_NONBLOCK

/* The event of file descriptor */
enum EPOLL_EVENTS { __EPOLL_DUMMY };
#define EPOLLIN 0x001
#define EPOLLPRI 0x002
#define EPOLLOUT 0x004
#define EPOLLRDNORM 0x040
#define EPOLLNVAL 0x020
#define EPOLLRDBAND 0x080
#define EPOLLWRNORM 0x100
#define EPOLLWRBAND 0x200
#define EPOLLMSG 0x400
#define EPOLLERR 0x008
#define EPOLLHUP 0x010
#define EPOLLRDHUP 0x2000
#define EPOLLEXCLUSIVE (1U<<28)
#define EPOLLWAKEUP (1U<<29)
#define EPOLLONESHOT (1U<<30)
#define EPOLLET (1U<<31)

/* The control type of file descriptor */
#define EPOLL_CTL_ADD 1
#define EPOLL_CTL_DEL 2
#define EPOLL_CTL_MOD 3

typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;

struct epoll_event {
uint32_t events;
epoll_data_t data;
};

int epoll_create(int);
int epoll_create1(int);
int epoll_ctl(int, int, int, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
int epoll_pwait(int, struct epoll_event *, int, int, const sigset_t *);

#ifdef __cplusplus
}
#endif

#endif /* MLIBC_SYS_EPOLL_H__ */
35 changes: 35 additions & 0 deletions components/libc/compilers/mlibc/smart/sys/eventfd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) mlibc & plct lab
*
* SPDX-License-Identifier: MIT
*
* Change Logs:
* Date Author Notes
* 2024/8/8 0Bitbiscuits the first version
*/
#ifndef MLIBC_SYS_EVENTFD_H__
#define MLIBC_SYS_EVENTFD_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <fcntl.h>

typedef uint64_t eventfd_t;

/* The type of file descriptor */
#define EFD_SEMAPHORE 1
#define EFD_CLOEXEC O_CLOEXEC
#define EFD_NONBLOCK O_NONBLOCK

int eventfd(unsigned int, int);
int eventfd_read(int, eventfd_t *);
int eventfd_write(int, eventfd_t);

#ifdef __cplusplus
}
#endif

#endif /* sys/eventfd.h */
Loading

0 comments on commit ca8de25

Please sign in to comment.