暂时在用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

  1. *  arch/arm/include/asm/byteorder.h
  2. *
  3. * ARM Endian-ness.  In little endian mode, the data bus is connected such
  4. * that byte accesses appear as:
  5. *  0 = d0...d7, 1 = d8...d15, 2 = d16...d23, 3 = d24...d31
  6. * and word accesses (data or instruction) appear as:
  7. *  d0...d31
  8. *
  9. * When in big endian mode, byte accesses appear as:
  10. *  0 = d24...d31, 1 = d16...d23, 2 = d8...d15, 3 = d0...d7
  11. * and word accesses (data or instruction) appear as:
  12. *  d0...d31
  13. */
  14. #ifndef __ASM_ARM_BYTEORDER_H
  15. #define __ASM_ARM_BYTEORDER_H
  16. #ifdef __ARMEB__
  17. #include <linux/byteorder/big_endian.h>
  18. #else
  19. #include <linux/byteorder/little_endian.h>
  20. #endif
  21. #endif

arch/mips/include/asm/byteorder.h

  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License.  See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996, 99, 2003 by Ralf Baechle
  7. */
  8. #ifndef _ASM_BYTEORDER_H
  9. #define _ASM_BYTEORDER_H
  10. #if defined(__MIPSEB__)
  11. #include <linux/byteorder/big_endian.h>
  12. #elif defined(__MIPSEL__)
  13. #include <linux/byteorder/little_endian.h>
  14. #else
  15. # error "MIPS, but neither __MIPSEB__, nor __MIPSEL__???"
  16. #endif
  17. #endif /* _ASM_BYTEORDER_H */

arch/powerpc/include/asm/byteorder.h

  1. #ifndef _ASM_POWERPC_BYTEORDER_H
  2. #define _ASM_POWERPC_BYTEORDER_H
  3. /*
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/byteorder/big_endian.h>
  10. #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中

  1. /**
  2. * __swab32 - return a byteswapped 32-bit value
  3. * @x: value to byteswap
  4. */
  5. #define __swab32(x)             \
  6. (__builtin_constant_p((__u32)(x)) ? \
  7. ___constant_swab32(x) :         \
  8. __fswab32(x))

宏定义展开,是一个条件判断符。

__builtin_constant_p是一个gcc的内建函数, 用于判断一个值在编译时是否是常数,如果参数是常数,函数返回 1,否则返回 0。
如果数据是常数,则__constant_swab32,实现如下:

  1. #define ___constant_swab32(x) ((__u32)(             \
  2. (((__u32)(x) & (__u32)0x000000ffUL) << 24) |        \
  3. (((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |        \
  4. (((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |        \
  5. (((__u32)(x) & (__u32)0xff000000UL) >> 24)))

对于常数数据,采用的是普通的位移然后拼接的方法,对于常数,这样的消耗是有必要的(这是kernel的解释,不是很理解)

如果数据是运行时计算数据,则使用__fswab32,实现如下:

  1. static inline __attribute_const__ __u32 __fswab32(__u32 val)
  2. {
  3. #ifdef __arch_swab32
  4. return __arch_swab32(val);
  5. #else
  6. return ___constant_swab32(val);
  7. #endif
  8. }

如果未定义__arch_swab32,则还是采用__constant_swab32方法翻转数据,但是arm mips ppc都定义了各自平台的__arch_swab32,来实现一个针对自己平台的高效的swap,分别定义如下:

arch/arm/include/asm/swab.h

  1. static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
  2. {
  3. __asm__ ("rev %0, %1" : "=r" (x) : "r" (x));
  4. return x;
  5. }

arch/mips/include/asm/swab.h

  1. static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
  2. {
  3. __asm__(
  4. "   wsbh    %0, %1          \n"
  5. "   rotr    %0, %0, 16      \n"
  6. : "=r" (x)
  7. : "r" (x));
  8. return x;
  9. }

arch/powerpc/include/asm/swab.h

  1. static inline __attribute_const__ __u32 __arch_swab32(__u32 value)
  2. {
  3. __u32 result;
  4. __asm__("rlwimi %0,%1,24,16,23\n\t"
  5. "rlwimi %0,%1,8,8,15\n\t"
  6. "rlwimi %0,%1,24,0,7"
  7. : "=r" (result)
  8. : "r" (value), "0" (value >> 24));
  9. return result;
  10. }

可以看出,arm使用1条指令(rev数据翻转指令),mips使用2条指令(wsbh rotr数据交换指令),ppc使用3条指令(rlwimi数据位移指令),来完成了32 bit数据的翻转。这相对于普通的位移拼接的方法要高效的多!

其实从函数名__fswab也可以看出是要实现fast swap的。

linux kernel 如何处理大小端的更多相关文章

  1. linux kernel如何处理大端小端字节序

    (转)http://blog.csdn.net/skyflying2012/article/details/43771179 最近在做将kernel由小端处理器(arm)向大端处理器(ppc)的移植的 ...

  2. Linux中判断大小端的一种方法

    大小端的定义无需赘言,常用的方法有使用联合体和指针法,如: int checkCPU() { union w { int a; char b; }c; c.a = 1; return (c.b == ...

  3. Linux内核中大小端判定宏

    #include <stdio.h> ];unsigned long mylong;} endian_test = { {'l','?','?','b'} }; #define ENDIA ...

  4. Linux大小端模式转换函数

    转自 http://www.cnblogs.com/kungfupanda/archive/2013/04/24/3040785.html 不同机器内部对变量的字节存储顺序不同,有的采用大端模式(bi ...

  5. htonl(),htons(),ntohl(),ntons()--大小端模式转换函数

    不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian). 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处. 小端模 ...

  6. [Linux] Big-endian and Little-endian (大小端模式)

    Big-endian Little-endian 大小端模式   https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...

  7. 如何处理错误消息Please install the Linux kernel header files

    Please install the Linux kernel "header" files matching the current kernel 当我启动minilkube时遇 ...

  8. 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/ ...

  9. Linux kernel的中断子系统之(八):softirq

    返回目录:<ARM-Linux中断系统>. 总结:中断分为上半部和下半部,上半部关中断:下半部开中断,处理可以延迟的事情.下半部有workqueue/softirq/tasklet三种方式 ...

随机推荐

  1. ajax获取其他网站接口信息

    MXS&Vincene  ─╄OvЁ  &0000023─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...

  2. [5] 智能指针boost::shared_ptr

    [1]boost::shared_ptr简介 boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_p ...

  3. property attribute: assign, strong, weak, unsafe_unretain and copy

    assign:用于“纯量类型”(如CGFloat 或 NSInteger等): strong:用于“对象类型”,定义了一种“拥有关系”(owning relationship),为这种属性设置新值时, ...

  4. Spring中@Controller和@RestController之间的区别

    1. Controller, RestController的共同点 都是用来表示Spring某个类的是否可以接收HTTP请求 2.  Controller, RestController的不同点 @C ...

  5. libevent源码分析(一)

    分析libevent的源代码,我的想法的是先分析各种结构体,struct event_base.struct event,然后是event_base_new函数.event_new函数.event_a ...

  6. 从yum源下载安装包及依赖包

    局域网内所有linux都用yum从外网源安装软件有点浪费,尤其遇到下载慢的情况: 所以考虑下载后传到其他机器安装,还可以保证版本一致(创建一个本地仓库更好,这个后面研究了再记录): 首先安装yum工具 ...

  7. Python正则表达式学习摘要及资料

    摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...

  8. HTML标签的应用(新手)

    雪碧图(精灵图): sprite compass-合并(尽量宽高相同) 兼容性: 1.resct重置技术:normalize技术 2.加前缀:-webkit- -moz- -0- -ms- 3.< ...

  9. GridControl

    隐藏Drag a column header here to group by that column https://www.devexpress.com/Support/Center/Questi ...

  10. CSS边距---盒子模型

    CSS盒子模型 盒子模型主要是有margin(外边距).border(边框).padding(内边距).content(内容)组成,这些属性我们可以把它转移到我们日常生活中的盒子上来理解,日常生活中所 ...