鴻蒙輕內核源碼分析:Newlib C

摘要:本文介紹了LiteOS-M內核Newlib C的實現,特別是文件系統和內存分配釋放部分,最後介紹了Newlib鉤子函數。

本文分享自華爲雲社區《鴻蒙輕內核M核源碼分析系列二十 Newlib C》,作者: zhushy。

使用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 文件操作接口

以下劃線開頭的函數實現是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);
}

在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;
}

2、Newlib C內存分配釋放

newlibc 的malloc適配參考The Red Hat newlib C Library-malloc。實現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);
}
......

可能已經注意到函數命名由__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",
]

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;
}

所有的可重入函數定義在文件夾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;
}

函數_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);

對於上文提到的函數_open,源碼如下。後續不再繼續分析了,LiteOS-M內核會提供這些鉤子函數的實現。

int
_open (const char * path, int flags, ...)
{
  return _swiopen (path, flags);
}

 

點擊關注,第一時間瞭解華爲雲新鮮技術~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章