鴻蒙輕內(nèi)核M核源碼分析系列之Newlib C
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
LiteOS-M內(nèi)核LibC實現(xiàn)有2種,可以根據(jù)需求進行二選一,分別是musl libC和newlibc。本文先學(xué)習(xí)下Newlib C的實現(xiàn)代碼。文中所涉及的源碼,均可以在開源站點https://gitee.com/openharmony/kernel_liteos_m 獲取。
使用Musl C庫的時候,內(nèi)核提供了基于LOS_XXX適配實現(xiàn)pthread、mqeue、fs、semaphore、time等模塊的posix接口(//kernel/liteos_m/kal/posix)。內(nèi)核提供的posix接口與musl中的標(biāo)準(zhǔn)C庫接口共同組成LiteOS-M的LibC。編譯時使用arm-none-eabi-gcc,但只使用其工具鏈的編譯功能,通過加上-nostdinc與-nostdlib強制使用我們自己改造后的musl-C。
社區(qū)及三方廠商開發(fā)多使用公版工具鏈arm-none-eabi-gcc加上私有定制優(yōu)化進行編譯,LiteOS-M內(nèi)核也支持公版arm-none-eabi-gcc C庫編譯內(nèi)核運行。newlib是小型C庫,針對posix接口涉及系統(tǒng)調(diào)用的部分,newlib提供一些需要系統(tǒng)適配的鉤子函數(shù),例如_exit(),_open(),_close(),_gettimeofday()等,操作系統(tǒng)適配這些鉤子,就可以使用公版newlib工具鏈編譯運行程序。
1、Newlib C文件系統(tǒng)
在使用Newlib C并且使能支持POSIX FS API時(可以在kernel\liteos-m\目錄下,執(zhí)行make meuconfig彈出配置界面,路徑為Compat-Choose libc implementation),如下圖所示??梢允褂梦募al\libc\newlib\porting\src\fs.c中定義的文件系統(tǒng)操作接口。這些是標(biāo)準(zhǔn)的POSIX接口,如果想了解POSIX用法,可以在linux平臺輸入 man -a 函數(shù)名稱,比如man -a opendir來打開函數(shù)的手冊。

1.1 函數(shù)mount、umount和umount2
這些函數(shù)的用法,函數(shù)實現(xiàn)和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 文件操作接口
以下劃線開頭的函數(shù)實現(xiàn)是newlib c的鉤子函數(shù)實現(xiàn)。有關(guān)newlib的鉤子函數(shù)調(diào)用過程下文專門分析下。
- 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時時,需要提供這些鉤子函數(shù)的空的實現(xiàn),返回-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內(nèi)存分配釋放
newlibc 的malloc適配參考The Red Hat newlib C Library-malloc。實現(xiàn)malloc適配有以下兩種方法:
- 實現(xiàn) _sbrk_r 函數(shù)。這種方法中,內(nèi)存分配函數(shù)使用newlib中的。
- 實現(xiàn) _malloc_r, _realloc_r, _free_r, _memalign_r, _malloc_usable_size_r等。這種方法中,內(nèi)存分配函數(shù)可以使用內(nèi)核的。
為了方便地根據(jù)業(yè)務(wù)進行內(nèi)存分配算法調(diào)優(yōu)和問題定位,推薦選擇后者。內(nèi)核的內(nèi)存函數(shù)定義在文件kal\libc\newlib\porting\src\malloc.c中。源碼片段如下,代碼實現(xiàn)比較簡單,不再分析源碼。
- ......
- 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);
- }
- ......
可能已經(jīng)注意到函數(shù)命名由__wrap_加上鉤子函數(shù)名稱兩部分組成。這是因為newlib中已經(jīng)存在這些函數(shù)的符號,因此需要用到gcc的wrap的鏈接選項替換這些函數(shù)符號為內(nèi)核的實現(xiàn),在設(shè)備開發(fā)板的配置文件中,比如//device/board/fnlink/v200zr/liteos_m/config.gni,新增這些函數(shù)的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鉤子函數(shù)介紹
以open函數(shù)的鉤子函數(shù)_open為例來介紹newlib的鉤子函數(shù)的調(diào)用過程。open()函數(shù)實現(xiàn)在newlib-cygwin\newlib\libc\syscalls\sysopen.c中,該函數(shù)會進一步調(diào)用函數(shù)_open_r,這是個可重入函數(shù)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;
- }
所有的可重入函數(shù)定義在文件夾newlib-cygwin\newlib\libc\reent,函數(shù)_open_r定義在該文件夾的文件newlib-cygwin\newlib\libc\reent\openr.c里。函數(shù)代碼如下:
- 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;
- }
函數(shù)_open_r如上述代碼所示,會進一步調(diào)用函數(shù)_open,該函數(shù),以arm硬件平臺為例,實現(xiàn)在newlib-cygwin\libgloss\arm\syscalls.c文件里。newlib目錄是和硬件平臺無關(guān)的痛毆他那個功能實現(xiàn),libloss目錄是底層的驅(qū)動實現(xiàn),以各個硬件平臺為文件夾進行組織。在特定硬件平臺的目錄下的syscalls.c文件里面實現(xiàn)了newlib需要的各個樁函數(shù):
- /* 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);
對于上文提到的函數(shù)_open,源碼如下。后續(xù)不再繼續(xù)分析了,LiteOS-M內(nèi)核會提供這些鉤子函數(shù)的實現(xiàn)。
- int
- _open (const char * path, int flags, ...)
- {
- return _swiopen (path, flags);
- }
小結(jié)
本文學(xué)習(xí)了LiteOS-M內(nèi)核Newlib C的實現(xiàn),特別是文件系統(tǒng)和內(nèi)存分配釋放部分,最后介紹了Newlib鉤子函數(shù)。