Data Types in the Kernel Sarah Diesburg COP 5641.

Slides:



Advertisements
Similar presentations
Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
Advertisements

Dynamic memory allocation
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
INSTRUCTION SET ARCHITECTURES
Bit Field.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Unix Network Programming Part 2: Elementary Sockets Jani Peusaari.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Arrays and Pointers in C Alan L. Cox
CSE378 MIPS ISA1 MIPS History MIPS is a computer family –R2000/R3000 (32-bit); R4000/4400 (64-bit); R8000; R10000 (64-bit) etc. MIPS originated as a Stanford.
Data Structures in the Kernel Sarah Diesburg COP 5641.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Types in the Kernel Ted Baker  Andy Wang CIS 4930 / COP 5641.
Instruction Set Architecture
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Appendix B Classifying Instruction Set Architecture Memory addressing mode Operations in the instruction set Control flow instructions Instruction format.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Stack and Heap Memory Stack resident variables include:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Lecture 8. MIPS Instructions #2 – Memory Access (Load/Store) Instructions Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer.
Lecture 4. MIPS Instructions #2 Memory Access (Load/Store) Instructions Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced Computer.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Instruction Set Architecture The portion of the machine visible to the programmer Issues: Internal storage model Addressing modes Operations Operands Encoding.
Byte Order Mohammad Kamal. Byte order Problem with byte order Numbers vs Data Practical example for reading data Exchanging Data between different systems.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Pointers *, &, array similarities, functions, sizeof.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
EECS 370 Discussion 1 xkcd.com. EECS 370 Discussion Topics Today: – ARM Addressing Endianness, Loading, and Storing Data – Data Layout Struct Packing.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Linux Kernel Development Memory Management Pavel Sorokin Gyeongsang National University
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Big-Endians Little-Endians and Bi-Endians
Data in Memory variables have multiple attributes symbolic name
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Socket Programming (Cont.)
Chapter7 Structure & C++
William Stallings Computer Organization and Architecture 8th Edition
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Pointers Lecture 2 Tue, Jan 24, 2006.
Computer Architecture
Java Programming Language
Java Basics Data Types in Java.
Pointer Arithmetic By Anand George.
Presentation transcript:

Data Types in the Kernel Sarah Diesburg COP 5641

Kernel Data Types For portability  Should compile with –Wall –Wstrict- prototypes flags Three main classes  Standard C types (e.g., int )  Explicitly sized types (e.g., u32 )  Types for specific kernel objects (e.g., pid_t )

Use of Standard C Types Normal C types are not the same size on all architectures Try misc-progs/datasize % misc-progs/datasize arch Size: char short int long ptr long-long u8 u16 u32 u64 i Try misc-modules/kdatasize to see kernel versions

Use of Standard C Types 64-bit platforms have different data type representations arch Size: char short int long ptr long-long u8 u16 u32 u64 i alpha armv4l ia m68k mips ppc sparc sparc x86_

Use of Standard C Types Knowing that pointers and long integers have the same size  Using unsigned long for kernel addresses prevents unintended pointer dereferencing

Assigning an Explicit Size to Data Items See  u8; /* unsigned byte (8-bits) */  u16; /* unsigned word (16-bits) */  u32; /* unsigned 32-bit value */  u64; /* unsigned 64-bit value */ If a user-space program needs to use these types, use __ prefix (e.g., __u8 )

Assigning an Explicit Size to Data Items Kernel also uses conventional types, such as unsigned int  Usually done for backward compatibility

Interface-Specific Types Interface-specific type: defined by a library to provide an interface to specific data structure (e.g., pid_t )

Interface-Specific Types Many _t types are defined in  Problematic in printk statements  One solution is to cast the value to the biggest possible type (e.g., unsigned long ) Avoids warning messages Will not lose data bits

Other Portability Issues Be suspicious of explicit constant values Most values are parameterized with preprocessor macros

Timer Intervals Do not assume 1000 jiffies per second  Scale times using HZ (number of interrupts per second) For example, check against a timeout of half a second, compare the elapsed time against HZ/2 Number of jiffies corresponding to msec second is always msec*HZ/1000

Page Size Memory page is PAGE_SIZE bytes, not 4KB  Can vary from 4KB to 64KB  PAGE_SHIFT contains the number of bits to shift an address to get its page number  See  User-space program can use getpagesize library function

Page Size Example  To allocate 16KB Should not specify an order of 2 to __ get_free_pages Use get_order #include int order = get_order(16*1024); buf = __get_free_pages(GFP_KERNEL, order);

Byte Order PC stores multibyte values low-byte first (little-endian) Some platforms use big-endian Use predefined macros 

Byte Order Examples  u32 cpu_to_le32(u32); cpu = internal CPU representation le = little endian  u64 be64_to_cpu(u64); be = big endian  U16 cpu_to_le16p(u16); p = pointer Converts value pointed to by p

Data Alignment How to read a 4-byte value stored at an address that is not a multiple of 4 bytes?  i386 permits this kind of access  Not all architectures permit it Can raise exceptions

Data Alignment Example char wolf[] = “Like a wolf”; char *p = &wolf[1]; unsigned long l = *(unsigned long *)p; Treats the pointer to a char as a pointer to an unsigned long, which might result in the 32- or 64- bit unsigned long value being loaded from an address that is not a multiple of 4 or 8, respectively.

Data Alignment Use the following typeless macros  #include  get_unaligned(ptr);  put_unaligned(val, ptr);

Data Alignment Another issue is the portability of data structures  Compiler rearranges structure fields to be aligned according to platform-specific conventions  Automatically add padding to make things aligned May no longer match the intended format

Data Alignment For example, consider the following structure on a 32-bit machine struct animal_struct { char dog; /* 1 byte */ unsigned long cat; /* 4 bytes */ unsigned short pig; /* 2 bytes */ char fox; /* 1 byte */ };

Data Alignment Structure not laid out like that in memory  Natural alignment of structure’s members is inefficient Instead, complier creates padding struct animal_struct { char dog; /* 1 byte */ u8 __pad0[3]; /* 3 bytes */ unsigned long cat; /* 4 bytes */ unsigned short pig; /* 2 bytes */ char fox; /* 1 byte */ u8 __pad1; /* 1 byte */ };

Data Alignment You can often rearrange the order of members in a structure to obviate the need for padding struct animal_struct { unsigned long cat; /* 4 bytes */ unsigned short pig; /* 2 bytes */ char dog; /* 1 byte */ char fox; /* 1 byte */ };

Data Alignment Another option is to tell the compiler to pack the data structure with no fillers added Example: struct { u16 id; u64 lun; u16 reserved1; u32 reserved2; } __attribute__ ((packed)) scsi; Without __attribute__ ((packed)), lun would be preceded by 2-6 bytes of fillers

Data Alignment No compiler optimizations Some compiler optimizations __attribute__ ((packed))

Pointers and Error Values Functions that return pointers cannot report negative error values  Return NULL on failure Some kernel interfaces encode error code in a pointer value  Cannot be compared against NULL  To use this feature, include

Pointers and Error Values To return an error, use  void *ERR_PTR(long error); To test whether a returned pointer is an error code, use  long IS_ERR(const void *ptr); To access the error code, use  long PTR_ERR(const void *ptr);