標籤:
最近在做將kernel由小端處理器(arm)向大端處理器(ppc)的移植的工作,現在kernel進入console穩定工作,基本工作已經完成,不過移植中有很多心得還是需要總結下,今天先將kernel對於大小端位元組序的處理來總結下。
之前寫過大小端位元組序的思考,文章連結地址:http://blog.csdn.net/skyflying2012/article/details/42065427。
根據之前的理解,位元組序可以認為是處理器主觀的概念,就像人如何去看待事物一樣,處理器分大端和小端,對於記憶體的讀寫,只要保證資料類型一致,就不存在位元組序的問題。
因此我感覺,位元組序不同造成的最大差異在於對於寄存器的讀寫。因為外設寄存器都是小端的(根據kernel代碼得出結論,下面還會在詳細解釋)
根據我之前位元組序思考的文章,對於寄存器讀寫差異,有2種方案:
(1)從硬體上解決這個問題,對於32位cpu,將32根資料匯流排反接,但是這樣對於定址小於32位元據可能有問題,並且不能所有模組都反接(如記憶體),這還涉及到編譯器的問題。
(2)從軟體上解決這個問題,在底層讀寫寄存器函數中,將讀/寫的資料進行swap。
作為軟體人員,我最關心第二種方案是否可行,因為在讀寫寄存器時對資料進行swap,增加了寄存器讀寫的複雜度,原來一條儲存/載入指令可以完成的工作,現在可能需要增加一些更swap相關的指令,無法保證寄存器操作的原子性了。對於高效能,大並發的系統,可能造成競態。
因此用最少的指令完成資料swap和r/w寄存器,才能保證linux系統正常穩定運行。
在移植bootloader中我是將資料進行位移來完成swap,因bootloader單進程,不會存在競態問題。
在kernel移植時很擔心這個問題,但是發現kernel下已經提供了大小端處理器操作寄存器時的通用函數,就是readl/writel(以操作32位寄存器為例)。
對於driver的開發人員不需要關心處理器的位元組序,寄存器操作直接使用readl/writel即可。
網上有很多文章提到readl/writel,但是沒有具體分析其實現。
今天就主要來分析下readl/writel如何?高效的資料swap和寄存器讀寫。我們就以readl為例,針對big-endian處理器,如何來對寄存器資料進行處理。
kernel下readl定義如下,在include/asm-generic/io.h
#define readl(addr) __le32_to_cpu(__raw_readl(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,我們在編寫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的。
我們反過來思考下,kernel針對小端處理器的寄存器讀寫資料沒有做任何處理,而對於大端處理器卻做了swap,這也說明了外設寄存器資料排布是小端位元組序的。
linux kernel如何處理大端小端位元組序