Linux kernel--kernel data type

Source: Internet
Author: User

Transferred from: http://www.linuxidc.com/Linux/2013-12/93637.htm

When migrating Linux to a new architecture, a number of issues that developers encounter are related to incorrect data types. Sticking with strict data types and compiling with-wall-wstrict-prototypes can avoid most bugs.

-wall Show all warnings
-wstrict-prototypes Rigorous detection of prototypes, if not consistent, warnings appear

The data types used by the kernel data are mainly divided into 3 types: standard C language types, size-determining types, and types of specific kernel objects.

standard C language type

When you need a 2-byte shim or a 4-byte string to represent something, you cannot use the standard C-language type because the size of the data type in C is different in different architectures. And some architectures, the size of the C data type of the kernel space and user space may also be different.

The address in the kernel is unsigned long, and the pointer size is the same as the long type.
Although the conceptual address is a pointer, using an unsigned integer allows for better memory management; The kernel sees physical memory as a giant array, and the memory address is the index of the array. We can easily take a pointer to a value, but when we deal with the memory address directly, we almost never value him in this way. The use of an integer type avoids this value and avoids the bug. So, with the fact that pointers and long integers are always the same size at least on all the platforms currently supported by Linux, the memory addresses in the kernel are often unsigned long.
The C99 standard defines the intptr_t and uintptr_t types, which are integer variables that can hold pointer values. But not nearly unused in the 2.6 kernel. Determine the type of size

Kernel: When you need to know the size of the data you define, you can use the following data types provided by the kernel:

U8; /* unsigned byte (8 bits) */
U16; /* unsigned word (bits) */
U32; /* Unsigned 32-bit value */
U64; /* Unsigned 64-bit value */
/* Although it is rarely necessary to have a signed type, but if necessary, just use s instead of u*/
User space: If you need to use these types for a program user space, you can add a double underscore to the symbol: __U8 and other types are defined independently of the __kernel__.interface-specific types (_t type)

The most commonly used data types in the kernel are declared by their own typedef, blocking any portability issues.
interface-specific (interface-specific) is a data type defined by a library to provide an interface for a particular data structure.
Note :

Recently, new interface-specific types have been rarely defined. There are many kernel developers who are no longer fond of using TypeDef statements, preferring to see the real type information directly used in the code.
Many of the older interface-specific types remain in the kernel, and they do not disappear quickly.
Even if you do not define an interface-specific type, you should always use a data type that is consistent and appropriate for the rest of the kernel. As long as the driver uses this "custom" type of function, but does not follow the convention, the compiler warns you, using the-wall compiler option and careful removal of all warnings, you can be confident that the code is portable.
main problems of _t type:
When printing them, it is often difficult to choose the correct PRINTK or printf format. The best way to print interface-specific data is to cast it to the maximum possible type (often a long or unsigned long) and print it in the appropriate format.
Time interval

When processing time intervals, do not assume the number of jiffies per second, not every Linux platform runs at a fixed speed. When calculating the time interval, use HZ (the number of timer interrupts per second) to calibrate your time.

hz:linux cores emit timer interrupt (IRQ 0) every fixed period, Span lang= "en-US" >hz is used to define timer interrupts each second several times. For example, hz is 1000, representing timer per second Interrupts.
tick:tic K is the reciprocal of hz, meaning timer interrupt the time of each interruption. such as hz for 250, tick for 4 milliseconds
jiffies : Jiffies for linux core variables timer interrupt,jiffies variables are added.
Page size

When using memory, remember that a memory page is page_size bytes, not 4KB. The associated macro definition is page_size and Page_shift (contains the number of bits that shift an address to get its page number). If the user space program requires this information, you can use the GetPageSize library function.
If a driver requires up to a few kilobytes of data to be staged, a portable workaround is Get_order:

#include <asm/page.h>
int order = Get_order (16*1024);
BUF = Get_free_pages (Gfp_kernel, order);
The/*get_order parameter must be a power of 2 */
BYTE storage Order

Do not assume the byte order. The code should be written in a way that does not depend on the byte order of the data being manipulated.
Header file Definition:

#ifdef __armeb__
#include linux/byteorder/big_endian.h>
#else
#include linux/byteorder/little_endian.h>
#endif
__big_endian is defined in <linux/byteorder/big_endian.h>, and __little_ is defined in <linux/byteorder/little_endian.h> ENDIAN, these processor-dependent byte sequences require encoding a bunch of conditional statements similar to #ifdef __litttle_endian when dealing with byte-order problems.

But there is a better way: The Linux kernel has a macro definition to handle the conversion between the processor byte order and the specific byte order. For example:
U32 cpu_to_le32 (U32);
U32 le32_to_cpu (U32);

/* These macro definitions convert the value used by a CPU to an unsigned 32-bit small head value, regardless of whether the CPU is big-endian or smaller, and whether it is a 32-bit processor or not. Returns the unmodified value when there is no conversion work to do. */

Data Alignment

The last question you should consider when writing portable code is how to access unaligned data. Access to misaligned data should use the following macros:
#include <asm/unaligned.h>
Get_unaligned (PTR);
Put_unaligned (Val, PTR);

These macros are untyped and are valid for each total data item, whether 1, 2, 4, or 8 bytes, and are defined in all kernel versions.

Another problem with alignment is the cross-platform portability of data structures. The same data structures may be compiled differently on different platforms. In order to write data structures that can be migrated across systems, you should always force the natural alignment of data items.
Natural alignment (natural alignment) refers to an integer multiple of the data item size that stores the data item on the address. You should use shims to avoid forcing self-??? A field that moves the data structure to the playhead compiler leaves holes in the structure.

For the good performance of the target processor, the compiler may silently insert the padding into the structure to ensure that each member is aligned. If you define a structure that matches the requirements of the device, the auto-shim breaks the intent. The solution to this problem is to tell the compiler that the structure must be "compact" and not add padding. For example, the following definitions:

struct
{
U16 ID;
U64 LUNs;
U16 Reserved1;
U32 Reserved2;
}
__ATTRIBUTE__ ((packed)) scsi;/* If the structure is compiled on a 64-bit platform, if there is no __attribute__ ((packed)), the LUN member may be preceded by a 2 or 6 padding byte. Pointers and Error values */
The actual application of this method can be found in the Spcaframe.h header file of the Servfox source code using the ARM9 and USB camera for video capture:
struct frame_t{
Char header[5];
int nbframe;
Double seqtimes;
int deltatimes;
int W;
int h;
int size;
int format;
unsigned short bright;
unsigned short contrast;
unsigned short colors;
unsigned short exposure;
unsigned char wakeup;
int acknowledge;
} __attribute__ ((packed));
struct client_t{
Char message[4];
unsigned char x;
unsigned char y;
unsigned char fps;
unsigned char updobright;
unsigned char updocontrast;
unsigned char updocolors;
unsigned char updoexposure;
unsigned char updosize;
unsigned char sleepon;
} __attribute__ ((packed));

detailed description of Linux Kernel : please click here
Linux Kernel : please click here.

Related reading :

The Linux kernel will replace iptables with Nftables http://www.linuxidc.com/Linux/2013-10/91727.htm

Linux 3.12 Code Suicidal Squirrel http://www.linuxidc.com/Linux/2013-09/90023.htm

How to install the Linux 3.11 kernel http://www.linuxidc.com/Linux/2013-09/89674.htm on Ubuntu

Ubuntu 13.10 (saucy salamander) kernel has been upgraded to Linux Kernel 3.10 RC5 http://www.linuxidc.com/Linux/2013-06/86110.htm

Linux Kernel 3.4.62 LTS is now available for download http://www.linuxidc.com/Linux/2013-09/90368.htm

How to install the Linux kernel on Ubuntu 13.10 3.12 http://www.linuxidc.com/Linux/2013-11/92930.htm

Linux kernel--kernel data type

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.