UClinux kernel and driver development

Source: Internet
Author: User
Introduction to uClinux kernel
• UClinux is the core branch of Linux 2.0.
Microcontroller for MMU Management Unit
• Inherits most of Linux's features
• Most Linux applications and drivers can be stored in uClinux
Run
• Streamlined Kernel
• Kernel <512kb
• Kernel + root <900kb
• Integrated into Linux 2.6 kernel

Differences between uClinux and Linux (1)
Memory Management
• UClinux runs on a processor without MMU
• Use flat memory management mode to virtualize the Intranet
Saving to physical memory is a one-to-one ing relationship.
• Use fixed stack space for applications. Loading
You need to relocate the application
• The size of available memory space is limited by physical memory
Differences between uClinux and Linux (2)
Fork and vfork
• Implement Multi-process management in uClinux through vfork
Currently, uClinux only supports vfork
• Vfork locks the parent process until the child process exec ()
Or exit ()
Note: The multithreading of uClinux applications can depend on
Implemented in the Standard C library (uclibc)

UClinux and uCOS-II (1)
• UClinux originated from Linux and is a complete system with a package
Including:
• Multi-task scheduling
• Memory Management
• File systems (and interfaces)
• Device Drivers
• Complete TCP/IP support
• Open source code and extensive support (GUI, FS, drivers, etc)
UClinux and uCOS-II (2)
UCOS-II
• Open source code, simple kernel, easy to learn and transplant
• Preemptible kernel with good real-time performance
• Only a simple kernel with multi-task scheduling
• Memory management is too simple and there is almost no dynamic memory management function
• Plug-ins are required for file systems and graphical interfaces
• No unified interface for device drivers
Real-time performance of uClinux
• UClinux kernel does not care about real-time performance issues
• Real-time support with RTLinux
• RTLinux processes real-time tasks. Non-real-time tasks are completed by Linux.
Cheng
• RTLinux provides real-time methods for Linux.
Applicable to uClinux. The RTLinux patch can be full
Fully meet uClinux's real-time requirements
Transplanted uClinux version
• Motorola Dragonball, 68 K
• Motorola ColdFire
• ARM7TDMI and arm9tdmi (arm940t)
• Altera NiO
•...
Www.uclinux.org
UClinux kernel Composition
• Initialization program segment (init) About 32 K
• Data Segment (data) 50 ~ About 100 K
• Uninitialized data segment (BSS) kb ~ About 150 K
• Code segment (text) about 300 KB
• (The init, Data, BSS, and text addresses are compiled
Determined by the positioning file vmlinux. LDS during the link)
• File System (romfs)> about 80 KB
Main Work of porting uClinux
• Select the cross compiler corresponding to the processor
• Select and modify bootloader
• Modify the link file (vmlinux-armv.lds.in) to locate each
Data segments
• Define the system timer and console)
• Write interrupt control functions
• Define the root file system
• Compile other system device drivers...

Mic
Compiler Selection
Compilation of uClinux kernel on ARM
• Cross Compiler
• Arm-elf-GCC and arm-Linux-gcc
• Standard C library-Applications
• UC-libc and uclibc
(UC) Linux bootloader
• System configuration, interrupt management, and boot
• Kernel loading, root file system, parameter passing, kernel debugging,
Download kernel and root file system
• Common bootloader for uClinux (Linux:
• Redboot
• Blob
• Vivi
• Uboot
• Armboot...

Understand the uClinux kernel source code
• The (UC) Linux kernel is huge and has a complex structure
• UClinux kernel statistics: nearly 10 thousand files, 4 million
Line Code
• Kernel programming habits (techniques) are different from applications

Mic
(UC) Linux kernel C code
• Linux kernel subjects use gnu c and are developed in ANSI C
Expanded
• The Linux kernel must be compiled and compiled by GCC.
• The GCC and Linux kernel versions are developed in parallel.
Highly Reliable
• Some programming skills used in kernel code
Rarely encountered in applications
Examples of gnu c Extension
• Inline and const keywords are absorbed from C ++
• If the ansi c code conflicts with the reserved keyword in gnu c
The problem can be solved by double underscores (_).
• For example, inline is equivalent to _ inline _ and ASM is equivalent
_ ASM __
• Struct (struct) initialization
Struct Initialization
Struct sample {
Int member_int;
Char * member_str;
Void (* member_fun) (void );
};
Implementation in ANSI C
Struct sample inst_c = {
100, // member_int
Null, // * member_str;
Myfunc // void (* member_fun) (void );
};
Implementation in c99
Struct sample inst_c99 = {
. Member_int = 100,
. Member_fun = myfun,
};
Implementation in GCC
Struct sample inst_gcc = {
Member_fun: myfun,
Member_int: 100,
};
Similar to the usage in c99, you do not have to worry about the definition of struct
The actual order and other undefined data in
It is advantageous when initializing a complex struct.

Mic
Flexible Use of macro definition (1)
• Although the inline keyword is defined in GCC, macro
Operation (# define) is still widely used in the system
• Example:
# Define dump_write (ADDR, NR) Do/
{Memcpy (bufp, ADDR, NR); bufp + = nR;} while (0)
Apply dump_write, just like using C functions:
If (ADDR)
Dump_write (ADDR, NR );
Else...
However, none of the definitions below can meet the above conditions.
Definition 1:
# Define dump_write (ADDR, NR) memcpy (bufp, ADDR, NR );/
Bufp + = nR;
Definition 2:
# Define dump_write (ADDR, NR) {memcpy (bufp, ADDR, NR );/
Bufp + = nR ;}
Flexible Use of macro definition (2)
# Define offsetof (strct, ELEM )/
(Long) & (struct strct *) 0)-> ELEM ))
• 1. (struct strct *) 0) strct pointer
• 2. & (struct strct *) 0)-> ELEM) Member address,
That is, the offset relative to 0.
• 3. Result: offsetof (strct, ELEM) returns the structure
Offset of the ELEM member in the body strct
Use of goto in C Language
• In application C programming, to ensure the program's module
Block, Goto is not recommended
• Kernel code must take into account the efficiency, so it is widely used.
Goto
• The ratio of the entire kernel is about one GOTO statement per 260 rows-
-Speed first
• Short-distance goto -- in the function

Mic
(UC) Linux driver
• Access to peripherals in Linux can only be obtained through the driver
• Linux provides unified interfaces for drivers, in the form of Files
Define the system driver:
• Open, release, read, write, IOCTL...
• Drivers are part of the kernel and can use interrupts, DMA, etc.
Operation
• The driver needs to transmit data between the user State and the kernel state
• In uClinux, you can directly access peripherals at the application layer and perform storage operations.
Port, but cannot handle the interruption-not recommended
• UClinux does not support module loading.
Kernel functions
• Process Management (Communication and synchronization between processes)
• Memory management (malloc/free)
• File System
• Device Control
• Network functions (network communication protocols, etc)
Categories of devices and modules in Linux
According to the features of the above system kernel
Devices are defined as the following three types:
• Character Devices
• Block devices
• Network Devices
Linux Devices
• Linux devices exist in the/dev directory as files
• The device file is a special file. You can use the LS/dev-l command.
See:
CrW ------- 1 Root 10, 7 Aug 31 2002 amigamouse1
CrW ------- 1 Root 10,134 Aug 31 2002 apm_bios
BRW-RW ---- 1 root disk 29, 0 Aug 31 2002 aztcm
Master and secondary device numbers
• The driver corresponding to the main device ID
• A driver can control several devices and raise the device number
Provides a way to differentiate them
• To add a driver to the system, you must assign it a master device.
. This assignment process is in the initialization process of the driver.
Int register_chrdev (unsigned int major, const char
* Name, struct file_operations * FoPs );
Create a device Node
• The device has been registered in the kernel table, and the access to the device passes through
Device File (the master device number of the device file and the device driver)
The kernel will call the correct function in the driver.
• Give the program a name that they can request the device driver.
This name must be inserted into the/dev directory, and
The primary device number is connected to the secondary device number.
• Use mknod to create a device node on the file system
Mknod
/Dev/mydevice
C 254 0
Dynamically allocate device numbers
• You can find it in the documentation/device.txt file.
To the list that has been statically allocated to most devices
• Because many numbers have been allocated, select one for the new device.
A unique number is very difficult.
• If Major is zero when register_chrdev is called,
The function selects an idle number and returns it as the return value.
Back
Dynamic Allocation
The dynamically assigned master device numbers cannot always be the same,
Unable to create a device node in advance
• Read from/proc/devices
CAT/proc/devices
• Use scripts to dynamically create Device File nodes
Device management problems
Today, Linux supports many different types of hardware. This
This means that there are hundreds of special files in/dev to indicate all
These devices. In addition, most of these special files are even
Not mapped to devices in the system
Use devfs
• Introduce devfs in Linux 2.4 kernel to solve
Problems with Device File Management in Linux
• Using the devfs_register () function in the driver
Create a node for the Device File System
• Mount the file system of the device when the system starts
• All required device nodes are automatically managed by the kernel.
Only mounted devices are in the/dev directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.