天天看點

鴻蒙輕核心源碼分析: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來打開函數的手冊。

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

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

點選關注,第一時間了解華為雲新鮮技術~

繼續閱讀