Embedded Systems 2/7

Embedded Systems 2/7

Embedded systems 2/7 J.-M Friedt Introduction Virtual memory access Embedded systems 2/7 Kernel module basics Using the kernel: timers J.-M Friedt Conclusion & lab session FEMTO-ST/d´epartement temps-fr´equence [email protected] slides at jmfriedt.free.fr September 9, 2020 1 / 24 Embedded systems 2/7 J.-M Friedt Operating system: the need for Introduction drivers Virtual memory access Kernel module basics Using the kernel: timers • Hardware abstraction: hide low level functions so that the developer Conclusion & lab session can focus on the functionalities provided by the peripheral ! a single entry point providing system calls (open, read, write, close) hiding access to hardware • Homogeneous interface to all peripherals (\Everything is a file") • Only the kernel can access hardware resources (DMA, interrupts) • Share resources and make sure only one process can access a given hardware function • Add functionalities to the Linux kernel: modules 2 / 24 Embedded systems 2/7 J.-M Friedt Virtual memory/hardware memory Introduction Hardware memory addressing Virtual memory • hardware memory: a value on the address bus identifies which access peripheral is active Kernel module basics • each peripheral decodes the address bus to detect whether it is the Using the kernel: target of a message timers • Conclusion & lab only one peripheral must match a given physical address (otherwise, session conflict) Virtual memory addressing • each process has its own address space • memory organization independent of physical constraints • dynamic loading binaries and associated libraries • MMU: translates between hardware and virtual memory addresses Virtual organization (process) Physical organization (CPU) 4096 Page 1 4096 Page 1 Page 2 Page 2 page & 0xff..f000 page & 0xff..f000 Page 3 Page 3 page & 0x00..0fff page & 0x00..0fff Page N Page N 3 / 24 Embedded systems 2/7 J.-M Friedt Hardware access from userspace Introduction Through /dev/mem Virtual memory access • advantage: not going through kernel abstraction layers (fast) Kernel module • drawback: not going through kernel abstraction layers (no handling basics Using the kernel: of concurrent memory access) timers #include < f c n t l . h> Conclusion & lab #include <s y s /mman . h> session #define MAP SIZE 4096UL // MMU page size #define MAP MASK ( MAP SIZE−1) // mask i n t main(int argc, char ∗∗ a r g v ) f i n t fd; void ∗ map base , ∗ v i r t a d d r ; u n s i g n e d long read result , writeval; o f f t target=0x12345678; // physical@ f d = open ( "/dev/mem" ,O RDWR j O SYNC) ; // MMU access map base=mmap(0 , MAP SIZE , PROT READ j PROT WRITE , n MAP SHARED, fd , t a r g e t & ~MAP MASK) ; v i r t a d d r=map base+(target & MAP MASK) ; // virt.@ r e a d r e s u l t =∗((unsigned long ∗) v i r t a d d r ) ; // read mem p r i n t f ( "0x%X(%p):0x%X\n" ,target ,virt a d d r , r e a d r e s u l t ) ; // ∗ ((unsigned long ∗) virt a d d r)= writeval;// write munmap( map base , MAP SIZE); close(fd); return 0; g 4 / 24 Embedded systems 2/7 J.-M Friedt The devmem tool Introduction Virtual memory access Kernel module basics Using the kernel: timers • Fast prototyping when accessing processor registers Conclusion & lab 1 session • Available in busybox • Use: Read/write from physical address ADDRESS Address to act upon WIDTH Width (8/16/...) VALUE Data to be written 1$BUILDROOT/output/build/busybox-1.30.1/miscutils/devmem.c 5 / 24 Embedded systems 2/7 J.-M Friedt Hardware access from the kernel Introduction Virtual memory • access Userspace: mmap on the /dev/mem pseudo-file Kernel module • Kernel space: ioremap function after requesting the address range basics Using the kernel: used by the peripheral timers #include <l i n u x / i o . h> // ioremap Conclusion & lab #define IO BASE 0xe000a000 // UG585p.1347 session and in the initialization function i f (request m e m r e g i o n ( IO BASE,0x2e4 , "GPIO test" )==NULL) printk (KERN ALERT "mem request failed" ); j m f gpio=(u32)ioremap(IO BASE, 0x2e4); // UG585p.1349 w r i t e l (1<<9, j m f gpio+0x204) ; // dir. of MIO9 To free the resource: r e l e a s e m e m r e g i o n ( IO BASE, 0x2e4); Check if the requested memory range is available (otherwise, kernel panic) 6 / 24 Embedded systems 2/7 J.-M Friedt Basic kernel module structure Introduction Kernel module = \plugin" requiring strarting and ending functions Virtual memory access #include <linux/module.h> /∗ Needed by all modules ∗/ Kernel module #include <linux/kernel.h> /∗ Needed for KERN INFO ∗/ basics #include <linux/init .h> /∗ Needed for the macros ∗/ Using the kernel: timers i n t hello s t a r t (void); Conclusion & lab session v o i d hello e n d (void); i n t hello s t a r t ( ) // init m o d u l e(void) f printk (KERN INFO "Hello\n" ); r e t u r n 0; g v o i d hello e n d ( ) // cleanup m o d u l e(void) f printk (KERN INFO "Goodbye\n" ); g m o d u l e i n i t ( h e l l o s t a r t ) ; m o d u l e e x i t ( h e l l o e n d ) ; No printf (write to /var/log/messages with printk accessible with dmesg), no floating point operation, no file operations. 7 / 24 Embedded systems 2/7 J.-M Friedt Kernel module compilation Introduction Virtual memory A kernel module must link to the running kernel: access • Kernel module requires kernel sources ($BR/output/build/linux*) or at least basics configured headers (linux-headers-X.Y.Z-amd64 on Using the kernel: timers Debian/GNU Linux) Conclusion & lab • dedicated Makefile calling the Linux kernel Makefile modules session method on PC: obj−m += mymod . o a l l : make −C /lib/modules/$ (shell uname −r ) / b u i l d n M=$ (PWD) modules or for cross-compiling, linking to the Buildroot $BR obj−m +=mymod . o a l l : make ARCH=arm CROSS COMPILE=arm−l i n u x − n −C $ (BR)/output/build/linux −XXX n M=$ (PWD) modules 8 / 24 Embedded systems 2/7 J.-M Friedt Communicating from userspace Introduction with the kernel Virtual memory access Kernel module Through /dev basics Using the kernel: • write, read, or control (ioctl) timers Conclusion & lab • each kernel module implements its own answer to the system calls session • each peripheral is identified by its class (major number) and its index (minor number) brw-rw---- 1 root disk 8, 0 Feb 28 06:21 sda brw-rw---- 1 root disk 8, 1 Feb 28 06:21 sda1 brw-rw---- 1 root disk 8, 2 Feb 28 06:21 sda2 brw-rw---- 1 root disk 8, 3 Feb 28 06:21 sda3 [...] crw-rw---- 1 root dialout 4, 64 Feb 28 07:21 ttyS0 crw-rw---- 1 root dialout 4, 65 Feb 28 07:21 ttyS1 crw-rw---- 1 root dialout 4, 66 Feb 28 07:21 ttyS2 crw-rw---- 1 root dialout 4, 67 Feb 28 07:21 ttyS3 In case an entry is missing (e.g. created by udev) in /dev : mknod 9 / 24 Embedded systems 2/7 2 J.-M Friedt Architecture of a POSIX Introduction compliant OS Virtual memory access Kernel module basics Using the kernel: Userspace timers Conclusion & lab applications (libc) session /dev/ttyUSB /dev/partport0 Module 1 Module 2 Module 3 (USB) (GPIO) (fs) Monolithic kernel (scheduler, interrupts, timers ...) system fonctions, networking, process scheduling and memory sharing, vfs 2/dev directory: pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap10.html 10 / 24 Embedded systems 2/7 J.-M Friedt The /dev directory Introduction • includes pseudo-files linking the kernel with the user space Virtual memory access • each device is defined with a major number and, within each class, Kernel module basics a minor number Using the kernel: • timers on the kernel side, a driver links to the same major number and Conclusion & lab provides implementations of the system calls session s t a t i c struct file operations fops= • f . r e a d : qadc read , open() . open : qadc open , • close() . u n l o c k e d i o c t l : q a d c i o c t l , • write() .release: qadc r e l e a s e , • read() // no.write on an ADC! • ioctl() g ; s t a t i c int i n i t q a d c i n i t (void) f ... r e g i s t e r chrdev (qadc major , "ppp" ! ,! , &f o p s ) ; ... g m o d u l e i n i t ( q a d c i n i t ) ; m o d u l e e x i t ( q a d c e x i t ) ; 11 / 24 Embedded systems 2/7 J.-M Friedt Input/Output Control (ioctl) Introduction • mechanism breaking the \Everything is a file” philosophy Virtual memory • access configures a peripheral by providing \what" index and as an Kernel module argument the \value" basics • no standardized command: the header file common to the driver Using the kernel: timers and the userspace program provides the list of IOCTL calls 3 Conclusion & lab • example: OSS (Open Sound System ) in session include/uapi/linux/soundcard.h of the kernel source: /∗ Use ioctl(fd, OSS GETVERSION,&int) to get the v e r s i o n number of the currently active driver.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    24 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us