linux kernel 如何处理大小端
暂时在用MPC8309,不太清楚大小端内核是什么时候给转的。
今天看了关于readl和writel具体实现的文章
今天就主要来分析下readl/writel如何实现高效的数据swap和寄存器读写。我们就以readl为例,针对big-endian处理器,如何来对寄存器数据进行处理。
kernel下readl定义如下,在include/asm-generic/io.h
#define readw(addr) __le32_to_cpu(__raw_readw(addr))
__raw_readl是最底层的寄存器读写函数,很简单,就从直接获取寄存器数据。来看__le32_to_cpu的实现,该函数针对字节序有不同的实现,对于小端处理器,在./include/linux/byteorder/little_endian.h中,如下:
#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
相当于什么都没做。而对于大端处理器,在./include/linux/byteorder/big_endian.h中,如下:
#define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
看字面意思也可以看出,__swab32实现数据翻转。等下我们就来分析__swab32的实现,精髓就在这个函数。
但是这之前先考虑一个问题,对于不同CPU,如arm mips ppc,怎么来选择使用little_endian.h还是big_endian.h的呢。
答案是,针对不同处理器平台,有arch/xxx/include/asm/byteorder.h头文件,来看下arm mips ppc的byteorder.h分别是什么。
arch/arm/include/asm/byteorder.h
- * arch/arm/include/asm/byteorder.h
- *
- * ARM Endian-ness. In little endian mode, the data bus is connected such
- * that byte accesses appear as:
- * 0 = d0...d7, 1 = d8...d15, 2 = d16...d23, 3 = d24...d31
- * and word accesses (data or instruction) appear as:
- * d0...d31
- *
- * When in big endian mode, byte accesses appear as:
- * 0 = d24...d31, 1 = d16...d23, 2 = d8...d15, 3 = d0...d7
- * and word accesses (data or instruction) appear as:
- * d0...d31
- */
- #ifndef __ASM_ARM_BYTEORDER_H
- #define __ASM_ARM_BYTEORDER_H
- #ifdef __ARMEB__
- #include <linux/byteorder/big_endian.h>
- #else
- #include <linux/byteorder/little_endian.h>
- #endif
- #endif
arch/mips/include/asm/byteorder.h
- /*
- * This file is subject to the terms and conditions of the GNU General Public
- * License. See the file "COPYING" in the main directory of this archive
- * for more details.
- *
- * Copyright (C) 1996, 99, 2003 by Ralf Baechle
- */
- #ifndef _ASM_BYTEORDER_H
- #define _ASM_BYTEORDER_H
- #if defined(__MIPSEB__)
- #include <linux/byteorder/big_endian.h>
- #elif defined(__MIPSEL__)
- #include <linux/byteorder/little_endian.h>
- #else
- # error "MIPS, but neither __MIPSEB__, nor __MIPSEL__???"
- #endif
- #endif /* _ASM_BYTEORDER_H */
arch/powerpc/include/asm/byteorder.h
- #ifndef _ASM_POWERPC_BYTEORDER_H
- #define _ASM_POWERPC_BYTEORDER_H
- /*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- */
- #include <linux/byteorder/big_endian.h>
- #endif /* _ASM_POWERPC_BYTEORDER_H */
可以看出arm mips在kernel下大小端都支持,arm mips也的确是可以选择处理器字节序。ppc仅支持big-endian。(其实ppc也是支持选择字节序的)
各个处理器平台的byteorder.h将littlie_endian.h/big_endian.h又包了一层,我们在编写driver时不需要关心处理器的字节序,只需要包含byteorder.h即可。
接下来看下最关键的__swab32函数,如下:
在include/linux/swab.h中
- /**
- * __swab32 - return a byteswapped 32-bit value
- * @x: value to byteswap
- */
- #define __swab32(x) \
- (__builtin_constant_p((__u32)(x)) ? \
- ___constant_swab32(x) : \
- __fswab32(x))
宏定义展开,是一个条件判断符。
__builtin_constant_p是一个gcc的内建函数, 用于判断一个值在编译时是否是常数,如果参数是常数,函数返回 1,否则返回 0。
如果数据是常数,则__constant_swab32,实现如下:
- #define ___constant_swab32(x) ((__u32)( \
- (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
- (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
- (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
- (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
对于常数数据,采用的是普通的位移然后拼接的方法,对于常数,这样的消耗是有必要的(这是kernel的解释,不是很理解)
如果数据是运行时计算数据,则使用__fswab32,实现如下:
- static inline __attribute_const__ __u32 __fswab32(__u32 val)
- {
- #ifdef __arch_swab32
- return __arch_swab32(val);
- #else
- return ___constant_swab32(val);
- #endif
- }
如果未定义__arch_swab32,则还是采用__constant_swab32方法翻转数据,但是arm mips ppc都定义了各自平台的__arch_swab32,来实现一个针对自己平台的高效的swap,分别定义如下:
arch/arm/include/asm/swab.h
- static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
- {
- __asm__ ("rev %0, %1" : "=r" (x) : "r" (x));
- return x;
- }
arch/mips/include/asm/swab.h
- static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
- {
- __asm__(
- " wsbh %0, %1 \n"
- " rotr %0, %0, 16 \n"
- : "=r" (x)
- : "r" (x));
- return x;
- }
arch/powerpc/include/asm/swab.h
- static inline __attribute_const__ __u32 __arch_swab32(__u32 value)
- {
- __u32 result;
- __asm__("rlwimi %0,%1,24,16,23\n\t"
- "rlwimi %0,%1,8,8,15\n\t"
- "rlwimi %0,%1,24,0,7"
- : "=r" (result)
- : "r" (value), "0" (value >> 24));
- return result;
- }
可以看出,arm使用1条指令(rev数据翻转指令),mips使用2条指令(wsbh rotr数据交换指令),ppc使用3条指令(rlwimi数据位移指令),来完成了32 bit数据的翻转。这相对于普通的位移拼接的方法要高效的多!
其实从函数名__fswab也可以看出是要实现fast swap的。
linux kernel 如何处理大小端的更多相关文章
- linux kernel如何处理大端小端字节序
(转)http://blog.csdn.net/skyflying2012/article/details/43771179 最近在做将kernel由小端处理器(arm)向大端处理器(ppc)的移植的 ...
- Linux中判断大小端的一种方法
大小端的定义无需赘言,常用的方法有使用联合体和指针法,如: int checkCPU() { union w { int a; char b; }c; c.a = 1; return (c.b == ...
- Linux内核中大小端判定宏
#include <stdio.h> ];unsigned long mylong;} endian_test = { {'l','?','?','b'} }; #define ENDIA ...
- Linux大小端模式转换函数
转自 http://www.cnblogs.com/kungfupanda/archive/2013/04/24/3040785.html 不同机器内部对变量的字节存储顺序不同,有的采用大端模式(bi ...
- htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian). 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处. 小端模 ...
- [Linux] Big-endian and Little-endian (大小端模式)
Big-endian Little-endian 大小端模式 https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...
- 如何处理错误消息Please install the Linux kernel header files
Please install the Linux kernel "header" files matching the current kernel 当我启动minilkube时遇 ...
- Linux Kernel 排程機制介紹
http://loda.hala01.com/2011/12/linux-kernel-%E6%8E%92%E7%A8%8B%E6%A9%9F%E5%88%B6%E4%BB%8B%E7%B4%B9/ ...
- Linux kernel的中断子系统之(八):softirq
返回目录:<ARM-Linux中断系统>. 总结:中断分为上半部和下半部,上半部关中断:下半部开中断,处理可以延迟的事情.下半部有workqueue/softirq/tasklet三种方式 ...
随机推荐
- Servlet-cookies机制
通过cookies,可以保存用户的使用习惯,优化用户体验,同时能减轻服务端压力.下面说下在Servlet中cookies机制的使用 就用保存用户登录数据来举例子: 打开网页的处理Servlet: pa ...
- python核心编程学习记录之序列(字符串元组列表)
可以在元组中放入可变类型如列表,然后修改列表来修改元组
- javascript,jQuery,trim()
JavaScript trim() Syntax string.trim() The trim() method removes whitespace from both sides of a str ...
- 接口测试之HttpClient
HttpClient使用详解 Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客 ...
- 探究platform_driver中的shutdown用途
http://blog.csdn.net/moxiaomomo/article/details/7897943 "quiesce" 说的也不太明确,我的猜测是:比如系统中有一个大功 ...
- python之redis
Redis简单介绍 如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构 ...
- 分布式算法系列——一致性Hash算法
摘自:http://www.blogjava.net/hello-yun/archive/2012/10/10/389289.html
- [问题2014A05] 复旦高等代数 I(14级)每周一题(第七教学周)
[问题2014A05] (1) 设 \(x_1,x_2\cdots,x_n,x\) 都是未定元, \(s_k=x_1^k+x_2^k+\cdots+x_n^k\,(k\geq 1)\), \(s_0 ...
- [问题2014S08] 复旦高等代数II(13级)每周一题(第八教学周)
[问题2014S08] 设分块上三角阵 \[A=\begin{bmatrix} A_1 & B \\ 0 & A_2 \end{bmatrix},\] 其中 \(m\) 阶方阵 \( ...
- Maven学习(四)-- 生命周期和插件
标签(空格分隔): 学习笔记 Maven生命周期是抽象的,不做任何实际的工作,在Maven的设计中,实际的任务都交由插件来完成. 每个构件步骤都可以绑定一个或者多个插件行为,而且Maven为大多数构建 ...