当前位置:   article > 正文

鸿蒙轻内核源码分析:Newlib C_鸿蒙源码

鸿蒙源码

使用 Musl C 库的时候,内核提供了基于 LOS_XXX 适配实现 pthread、mqeue、fs、semaphore、time 等模块的 posix 接口(//kernel/liteos_m/kal/posix)。内核提供的 posix 接口与 musl 中的标准 C 库接口共同组成 LiteOS-M 的 LibC。编译时使用 arm-none-eabi-gcc,但只使用其工具链的编译功能,通过加上 - nostdinc 与 - nostdlib 强制使用我们自己改造后的 musl-C。

社区及三方厂商开发多使用公版工具链 arm-none-eabi-gcc 加上私有定制优化进行编译,LiteOS-M 内核也支持公版 arm-none-eabi-gcc C 库编译内核运行。newlib 是小型 C 库,针对 posix 接口涉及系统调用的部分,newlib 提供一些需要系统适配的钩子函数,例如_exit (),_open (),_close (),_gettimeofday () 等,操作系统适配这些钩子,就可以使用公版 newlib 工具链编译运行程序。

1、Newlib C 文件系统

在使用 Newlib C 并且使能支持 POSIX FS API 时(可以在 kernel\liteos-m\ 目录下,执行 make meuconfig 弹出配置界面,路径为 Compat-Choose libc implementation),如下图所示。可以使用文件 kal\libc\newlib\porting\src\fs.c 中定义的文件系统操作接口。这些是标准的 POSIX 接口,如果想了解 POSIX 用法,可以在 linux 平台输入 man -a 函数名称,比如 man -a opendir 来打开函数的手册。

1.1 函数 mount、umount 和 umount2

这些函数的用法,函数实现和 musl c 部分一致。

int mount(const char *source, const char *target,
          const char *filesystemtype, unsigned long mountflags,
          const void *data)
{
    return LOS_FsMount(source, target, filesystemtype, mountflags, data);
}

int umount(const char *target)
{
    return LOS_FsUmount(target);
}

int umount2(const char *target, int flag)
{
    return LOS_FsUmount2(target, flag);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.2 文件操作接口

以下划线开头的函数实现是 newlib c 的钩子函数实现。有关 newlib 的钩子函数调用过程下文专门分析下。

int _open(const char *path, int oflag, ...)
{
    va_list vaList;
    va_start(vaList, oflag);
    int ret;
    ret = LOS_Open(path, oflag);
    va_end(vaList);
    return ret;
}

int _close(int fd)
{
    return LOS_Close(fd);
}

ssize_t _read(int fd, void *buf, size_t nbyte)
{
    return LOS_Read(fd, buf, nbyte);
}

ssize_t _write(int fd, const void *buf, size_t nbyte)
{
    return LOS_Write(fd, buf, nbyte);
}

off_t _lseek(int fd, off_t offset, int whence)
{
    return LOS_Lseek(fd, offset, whence);
}

int _unlink(const char *path)
{
    return LOS_Unlink(path);
}

int _fstat(int fd, struct stat *buf)
{
    return LOS_Fstat(fd, buf);
}

int _stat(const char *path, struct stat *buf)
{
    return LOS_Stat(path, buf);
}

int fsync(int fd)
{
    return LOS_Fsync(fd);
}

int mkdir(const char *path, mode_t mode)
{
    return LOS_Mkdir(path, mode);
}

DIR *opendir(const char *dirName)
{
    return LOS_Opendir(dirName);
}

struct dirent *readdir(DIR *dir)
{
    return LOS_Readdir(dir);
}

int closedir(DIR *dir)
{
    return LOS_Closedir(dir);
}

int rmdir(const char *path)
{
    return LOS_Unlink(path);
}

int rename(const char *oldName, const char *newName)
{
    return LOS_Rename(oldName, newName);
}

int statfs(const char *path, struct statfs *buf)
{
    return LOS_Statfs(path, buf);
}

int ftruncate(int fd, off_t length)
{
    return LOS_Ftruncate(fd, length);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

在 newlib 没有使能使能支持 POSIX FS API 时时,需要提供这些钩子函数的空的实现,返回 - 1 错误码即可。

int _open(const char *path, int oflag, ...)
{
    return -1;
}

int _close(int fd)
{
    return -1;
}

ssize_t _read(int fd, void *buf, size_t nbyte)
{
    return -1;
}

ssize_t _write(int fd, const void *buf, size_t nbyte)
{
    return -1;
}

off_t _lseek(int fd, off_t offset, int whence)
{
    return -1;
}

int _unlink(const char *path)
{
    return -1;
}

int _fstat(int fd, struct stat *buf)
{
    return -1;
}

int _stat(const char *path, struct stat *buf)
{
    return -1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

2、Newlib C 内存分配释放

实现 malloc 适配有以下两种方法:

  • 实现 _sbrk_r 函数。这种方法中,内存分配函数使用 newlib 中的。
  • 实现 _malloc_r, _realloc_r, _free_r, _memalign_r, _malloc_usable_size_r 等。这种方法中,内存分配函数可以使用内核的。

为了方便地根据业务进行内存分配算法调优和问题定位,推荐选择后者。内核的内存函数定义在文件 kal\libc\newlib\porting\src\malloc.c 中。源码片段如下,代码实现比较简单,不再分析源码。

......
void __wrap__free_r(struct _reent *reent, void *aptr)
{
    if (aptr == NULL) {
        return;
    }

    LOS_MemFree(OS_SYS_MEM_ADDR, aptr);
}

size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr)
{
    return 0;
}

void *__wrap__malloc_r(struct _reent *reent, size_t nbytes)
{
    if (nbytes == 0) {
        return NULL;
    }

    return LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
}

void *__wrap__memalign_r(struct _reent *reent, size_t align, size_t nbytes)
{
    if (nbytes == 0) {
        return NULL;
    }

    return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, nbytes, align);
}
......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

可能已经注意到函数命名由__wrap_加上钩子函数名称两部分组成。这是因为 newlib 中已经存在这些函数的符号,因此需要用到 gcc 的 wrap 的链接选项替换这些函数符号为内核的实现,在设备开发板的配置文件中,比如 //device/board/fnlink/v200zr/liteos_m/config.gni,新增这些函数的 wrap 链接选项,示例如下:

board_ld_flags += [
     "-Wl,--wrap=_malloc_r",
     "-Wl,--wrap=_realloc_r",
     "-Wl,--wrap=_free_r",
     "-Wl,--wrap=_memalign_r",
     "-Wl,--wrap=_malloc_usable_size_r",
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3、Newlib 钩子函数介绍

以 open 函数的钩子函数_open 为例来介绍 newlib 的钩子函数的调用过程。open () 函数实现在 newlib-cygwin\newlib\libc\syscalls\sysopen.c 中,该函数会进一步调用函数_open_r,这是个可重入函数 Reentrant Function,支持在多线程中运行。

int
open (const char *file,
        int flags, ...)
{
  va_list ap;
  int ret;

  va_start (ap, flags);
  ret = _open_r (_REENT, file, flags, va_arg (ap, int));
  va_end (ap);
  return ret;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

所有的可重入函数定义在文件夹 newlib-cygwin\newlib\libc\reent,函数_open_r 定义在该文件夹的文件 newlib-cygwin\newlib\libc\reent\openr.c 里。函数代码如下:

int
_open_r (struct _reent *ptr,
     const char *file,
     int flags,
     int mode)
{
  int ret;

  errno = 0;
  if ((ret = _open (file, flags, mode)) == -1 && errno != 0)
    ptr->_errno = errno;
  return ret;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

函数_open_r 如上述代码所示,会进一步调用函数_open,该函数,以 arm 硬件平台为例,实现在 newlib-cygwin\libgloss\arm\syscalls.c 文件里。newlib 目录是和硬件平台无关的痛殴他那个功能实现,libloss 目录是底层的驱动实现,以各个硬件平台为文件夹进行组织。在特定硬件平台的目录下的 syscalls.c 文件里面实现了 newlib 需要的各个桩函数:

/* Forward prototypes.  */
int	_system		(const char *);
int	_rename		(const char *, const char *);
int	_isatty		(int);
clock_t _times		(struct tms *);
int	_gettimeofday	(struct timeval *, void *);
int	_unlink		(const char *);
int	_link		(const char *, const char *);
int	_stat		(const char *, struct stat *);
int	_fstat		(int, struct stat *);
int	_swistat	(int fd, struct stat * st);
void *	_sbrk		(ptrdiff_t);
pid_t	_getpid		(void);
int	_close		(int);
clock_t	_clock		(void);
int	_swiclose	(int);
int	_open		(const char *, int, ...);
int	_swiopen	(const char *, int);
int	_write		(int, const void *, size_t);
int	_swiwrite	(int, const void *, size_t);
_off_t	_lseek		(int, _off_t, int);
_off_t	_swilseek	(int, _off_t, int);
int	_read		(int, void *, size_t);
int	_swiread	(int, void *, size_t);
void	initialise_monitor_handles (void);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

对于上文提到的函数_open,源码如下。后续不再继续分析了,LiteOS-M 内核会提供这些钩子函数的实现。

int
_open (const char * path, int flags, ...)
{
  return _swiopen (path, flags);
}
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/293105
推荐阅读
相关标签
  

闽ICP备14008679号