1、Unlocking the Power of POSIX Support in Zephyr RTOSAlexey BrodkinSynopsysJune 28th2023,Prague 2023 Synopsys,Inc.2Alexey BrodkinEngineering manager at Synopsys5 years in Zephyr developmentManage Zephyr development in SynopsysZephyr ambassador 2023 Synopsys,Inc.3Agenda Introduction:Zephyr Project&POSI
2、X POSIX Support in Zephyr RTOS Use-cases 2023 Synopsys,Inc.4Introduction:Zephyr Project&POSIXZephyr ProjectPOSIX 2023 Synopsys,Inc.5Features overviewKernelOS ServicesApplication ServicesHAL3rd Party LibrariesZephyr OSComprehensive,lightweight,kernel&supporting servicesFits where Linux is too bigInhe
3、rently portable&secureHighly connectedBluetooth 5.0&BLEWi-Fi,Ethernet,CANbus,IoT protocols:CoAP,LwM2M,MQTT,OpenThread,USB&USB-CDeveloper-friendlyLogging,tracing,debugging,built-in shell,Windows/Linux/macOS support,2023 Synopsys,Inc.6Architecture 2023 Synopsys,Inc.7Basic facts about POSIX Acronym for
4、 Portable Operating System Interface.IEEE Std 1003.1“Portable Operating System Interface(POSIX)”Originally focused on the UNIX systems IEEE Std 1003.13“Standardized Application Environment Profile(AEP)POSIX Realtime and Embedded Application Support”First version in 1988,latest version in 2018 Specif
5、ies application programming interfaces(APIs)at the source level,and is about source code portability,not binary compatibilityPOSIX threads pthread_create()pthread_join()pthread_mutex_lock()pthread_cond_wait()Device I/O functions open()close()fputc()fputc()Basic C language functions abs()atoi()div()r
6、and()2023 Synopsys,Inc.8POSIX in the context of embedded systemsIts all about source code portabilityLiteOSINTEGRITY RTOSPortable,cross platforms librariesSame application running on multiple platformsOS/execution environment independenceVxWorks is a registered trademark of Wind River Systems,Inc.,F
7、reeRTOSTMis a registered trademark of Amazon Web Services,Inc.,INTEGRITY is a registered trademark of Green Hills Software,NuttX is a registered trademark of the Apache Software Foundation,LYNXOS is a registered trademark of Lynx Software Technologies Inc.,Nucleus RTOS is a registered trademark of S
8、iemens Industry Software Inc.,QNX is a registered trademark of BlackBerry Limited 2023 Synopsys,Inc.9POSIX.13 Realtime and Embedded Application SupportIEEE Std 1003.13-2003,Realtime System Profiles:PSE51-54 PSE54Multi-purpose real-timePSE53Dedicated real-timePSE52Realtime controllerPSE51Minimal real
9、-time Shell&utilities Multiple users Full file system Networking Asynchronous I/O Multiple processes Simple file system Message queues Tracing C language support Signals Single process Threads 2023 Synopsys,Inc.10POSIX Support in Zephyr RTOSThe history of POSIX support in Zephyr RTOSOverview of Zeph
10、yrs POSIX implementation“POSIX Roadmap for LTSv3”by Chris Friedton June 29th 2023 Synopsys,Inc.11 2023 Synopsys,Inc.11History of POSIX support in Zephyr RTOS2017 Jul:POSIX threads base(barriers,condition variables,mutexes)Oct:“native_posix”board2018 Feb:POSIX clock,sleep,timers,threads Mar:POSIX sem
11、aphores Apr:POSIX message queue May:Add Posix Style File System API support2019 Jul:Civetweb HTTP sample Jul:Docs about POSIX implementation in Zephyr2022 Jun:Civetweb sample removed2023 Feb:GSoC project“HTTP server for Zephyr”Jan-:Significant overhaul of POSIX support by Chris FriedtMaintainers You
12、vedeep Singh Niranjhana Narayanan Ramakrishna Pallala Paul Sokolovsky Chris Friedt 2023 Synopsys,Inc.12 2023 Synopsys,Inc.12POSIX in Zephyr RTOS POSIX_C_LANG_SUPPORT abs(),calloc(),free()etc.POSIX_THREADS_BASE pthread_attr_*()pthread_cancel()pthread_create()pthread_detatch()pthread_cond_*()pthread_m
13、utex_*()POSIX_SIGNALS abort()POSIX_DEVICE_IO fprintf(),fputc(),fputs(),fwrite()open(),perror(),printf(),putc(),puts(),read()vfprintf(),vprintf()HardwareBSPZephyr kernelApplicationMiddlewareNetworkingFile systemPOSIX PSE51POSIX PSE52POSIX PSE53https:/docs.zephyrproject.org/latest/services/portability
14、/posix.html 2023 Synopsys,Inc.13Limitations and differences from traditional POSIXIts yet another abstraction layerAutomatic pthread stack and object allocationSome headers need to be moved or harmonized with native Zephyr headersMore C library functions need to be implemented or updated their curre
15、nt behaviorsysconf()utility is missing 2023 Synopsys,Inc.14Real examples of Zephyr RTOS with POSIX supportBuilt-in sample:Civet Web3rdparty application:EEMBC CoreMark Pro 2023 Synopsys,Inc.15HTTP web-server example:Civet Web$west build-b qemu_arc_hssamples/net/civetweb/http_server$west build-t run*B
16、ooting Zephyr OS build zephyr-v3.1.0*net_config:Initializing network net_config:IPv4 address:192.0.2.1 mg_cry_internal_impl:log_access:192.0.2.2-GET/?null HTTP/1.0 200 2023 Synopsys,Inc.16Civet Web:Complex application on top of Zephyr RTOSWritten mostly on C and uses:POSIX API POSIX Threads POSIX Me
17、ssage Queues BSD Sockets DNS client IPv4 TCP Ethernet device drivers Independent project developed on GitHub:https:/ Very minimal changes for Zephyr support,see commit 67cdc Removed from Zephyr RTOS in v3.2.0 due to missing maintainer Replacement to be worked onduring GSoC 2023Services and APIs avai
18、lable in Zephyr RTOS make it possible 2023 Synopsys,Inc.17EEMBC CoreMark ProPrimer for easy porting of POSIX compliant application to Zephyr RTOSInput data Original sources:https:/ Uses EEMBCs Multi-Instance Test Harness(MITH)MITH abstraction layer is configured to work with the POSIX pthreadPort to
19、 Zephyr Integrate with Zephyr build system*CMakeLists.txtprj.conf Pre-populate argc&*argvRequires porting!*Zephyr freestanding application:https:/docs.zephyrproject.org/latest/develop/application/index.html#zephyr-freestanding-app 2023 Synopsys,Inc.18CoreMark Pro:Integrate with Zephyr build systemcm
20、ake_minimum_required(VERSION 3.20.0)find_package(Zephyr REQUIRED HINTS$ENVZEPHYR_BASE)project(core)#MITH headersinclude_directories(mith/include)include_directories(mith/al/include)#MITH sorcesFILE(GLOB mith_sources mith/al/src/*.c mith/src/*.c)target_sources(app PRIVATE$mith_sources)#Input argument
21、starget_sources(app PRIVATE main-args.c)#CoreMark Pro sourcesFILE(GLOB app_sources benchmarks/core/*.c)target_sources(app PRIVATE$app_sources)target_sources(app PRIVATE workloads/core/core.c)set_ifndef(W_WORKERS_NUM 4)target_compile_options(app PRIVATE-D W_WORKERS_NUM=$W_WORKERS_NUMset_ifndef(C_CONT
22、EXT_NUM 4)target_compile_options(app PRIVATE-D C_CONTEXT_NUM=$C_CONTEXT_NUMset_ifndef(I_ITERATION_NUM 1)target_compile_options(app PRIVATE-D I_ITERATION_NUM=$I_ITERATION_NUMCMakeLists.txt 2023 Synopsys,Inc.19#Enable POSIX APICONFIG_POSIX_API=yCONFIG_PTHREAD_IPC=yCONFIG_POSIX_CLOCK=y#Disable getopt()
23、in Zephyr,as CoreMark uses its own version#CONFIG_GETOPT is not set#Use full-featured libcCONFIG_NEWLIB_LIBC=y#Optimize for speed(default is for size)CONFIG_SPEED_OPTIMIZATIONS=yCoreMark Pro:Integrate with Zephyr build systemprj.conf 2023 Synopsys,Inc.20CoreMark Pro:Pre-populate argc&*argv/main-args
24、.c#define _g_xstr(a)_g_str(a)#define _g_str(a)#aint argc=5;char*argv=core.exe,-v0,-w _g_xstr(W_WORKERS_NUM),-c _g_xstr(C_CONTEXT_NUM),-i _g_xstr(I_ITERATION_NUM);/workloads/core/core.c#ifdef _ZEPHYR_extern int argc;extern char*argv;void main(void)#elseint main(int argc,char*argv)#endifchar nameMITH_
25、MAX_NAME;char dataname_bufMITH_MAX_NAME;.Embedded application doesnt accept command line arguments,work it around 2023 Synopsys,Inc.21CoreMark Pro:execute“core”benchmarkwest build-b qemu_arc_hs core-t run-west build:running target run*Booting Zephyr OS build zephyr-v3.4.0*-Info:Starting Run.-Workloa
26、d:core=490760323-core:time(ns)=1137920-core:contexts=4-core:workers=4-core:iterations=4-core:time(secs)=1137.92-core:secs/workload=284.48-core:workloads/sec=0.00351519-Done:core=490760323 2023 Synopsys,Inc.22ConclusionOngoing work since 2017 done by multiple peoplePOSIX support development in Zephyr
27、 is driven by real application needs,i.e.enough POSIX features to get application X working on ZephyrA lot could be done with POSIX compatibility layer in Zephyr alreadyThere are pros&cons of using POSIXThank You 2023 Synopsys,Inc.24Backup slides 2023 Synopsys,Inc.25Additional resources“POSIX Roadma
28、p for LTSv3”,presentation by Chris Friedt on June 29th,14:10 CET Standards documentation 1003.1-2017 IEEE Standard for Information Technology Portable Operating System Interface(POSIX(TM)Base Specifications,Issue 7 1003.13-2003 IEEE Standard for Information Technology Standardized Application Enviro
29、nment Profile(AEP)POSIX(TM)Realtime and Embedded Application Support Interesting material Early experience with POSIX 1003.4 and POSIX 1003.4 A by B.O.Gallmeister,Lynx Real-Time Systems“Isnt it About Time for a Standard RTOS API?”article by Bill Lamie(father of ThreadX&PX5)The Open Group POSIX Profi
30、le 52 Test Suite VSPSE52-2003“Real-time POSIX:An overview”by Michael Gonzlez Harbour,Departamento de Electrnica,Universidad de Cantabria GitHub issues on the matter:RFC Proposed development plan for Zephyrs POSIX subsystem RFC:POSIX Roadmap for LTSv3 2023 Synopsys,Inc.26List of POSIX Base StandardsP
31、OSIX.1 System Interface(basic reference standard)POSIX.2 Shell and UtilitiesPOSIX.3 Methods for Testing Conformance to POSIXPOSIX.4 Real-time ExtensionsPOSIX.4aThreads ExtensionsPOSIX.4bAdditional Real-time ExtensionsPOSIX.6 Security ExtensionsPOSIX.7 System AdministrationPOSIX.8 Transparent File Ac
32、cessPOSIX.12Protocol Independent Network InterfacesPOSIX.15Batch Queuing ExtensionsPOSIX.17Directory Services 2023 Synopsys,Inc.27List of POSIX Application Environment StandardsPOSIX.0Guide to POSIX Open System EnvironmentPOSIX.10 Supercomputing Application Environment ProfilePOSIX.11Transaction Pro
33、cessing Application Environment ProfilePOSIX.13Real-time Application Environment ProfilesPOSIX.14Multiprocessing Application Environment ProfilePOSIX.18POSIX Platform Application Environment Profile 2023 Synopsys,Inc.28Native POSIX execution(native_posix)Convenient tool for Zephyr RTOS exploration w
34、ith great visibility of host tools Designed and tested to run in Linux Produces native binary for the host Each Zephyr thread is mapped to one POSIX Thread Transparent to application Allow functional debugging,instrumentation and analysis of the code with native toolingNormalZephyr layering“Native_posix”Zephyr layeringhttps:/docs.zephyrproject.org/latest/boards/posix/doc/arch_soc.html