kernel: add common infrastructure for unaligned access
PATCH 1/2 kernel: add common infrastructure for unaligned access
Create a linux/unaligned folder similar in spirit to the linux/byteorder folder to hold generic implementations collected from various arches.
Currently there are five implementations:
1) cpu_endian.h: C-struct based, from asm-generic/unaligned.h
2) little_endian.h: Open coded byte-swapping, heavily based on asm-arm
3) big_endian.h: Open coded byte-swapping, heavily based on asm-arm
4) no_builtin_memcpy.h: taken from multiple implementations in tree
5) access_ok.h: taken from x86 and others, unaligned access is ok.
All of the new implementations checks for sizes not equal to 1,2,4,8 and will BUG at compile time.
API additions:
get_unaligned_{le16|le32|le64|be16|be32|be64}(p) which is meant to replace code of the form:le16_to_cpu(get_unaligned((__le16 *)p));
put_unaligned_{le16|le32|le64|be16|be32|be64}(val, pointer) which is meant to replace code of the form:put_unaligned(cpu_to_le16(val), (__le16 *)p);
Headers to create these based on the selected implementation and defining the appropriate get_unaligned() and put_unaligned() macros are:
generic_le.h: Use the C-struct for get/put_unaligned and the le helpers, use the opencoded be byteswapping implementation for big-endian.
generic_be.h: Use the C-struct for get/put_unaligned and the be helpers, use the opencoded le byteswapping implementation for little-endian.
generic.h: Use opencoded byteswapping for all helpers, leaves it to the arch to define get/put_unaligned.
---
Removed some superfluous casts noticed by David Howells. Made all arches check the only size 1,2,4,8 types are passed to get/put_unaligned.
include/linux/unaligned/access_ok.h | 98 +++++++++++++++++++++++++ include/linux/unaligned/big_endian.h | 86 ++++++++++++++++++++++ include/linux/unaligned/cpu_endian.h | 92 ++++++++++++++++++++++++ include/linux/unaligned/generic.h | 67 +++++++++++++++++ include/linux/unaligned/generic_be.h | 70 ++++++++++++++++++ include/linux/unaligned/generic_le.h | 70 ++++++++++++++++++ include/linux/unaligned/little_endian.h | 86 ++++++++++++++++++++++ include/linux/unaligned/no_builtin_memcpy.h | 102 +++++++++++++++++++++++++++ 8 files changed, 671 insertions(+), 0 deletions(-)
include/linux/unaligned/access_ok.h new file +98 -0
@@ -0,0 +1,98 @@ +#ifndef _LINUX_UNALIGNED_ACCESS_OK_H_ +#define _LINUX_UNALIGNED_ACCESS_OK_H_ + +#include <linux/kernel.h> +#include <asm/byteorder.h> + +extern void __build_bug_unaligned_length (void ); + +#define get_unaligned (ptr) ({ \ + typeof(*(ptr)) __tmp; \ + switch (sizeof(__tmp)) { \ + case 1: \ + case 2: \ + case 4: \ + case 8: \ + __tmp = *(ptr); \ + break; \ + default: \ + __build_bug_unaligned_length(); \ + break; \ + } \ + __tmp; }) + +#define put_unaligned (val, ptr) ({ \ + switch (sizeof(*(ptr))) { \ + case 1: \ + case 2: \ + case 4: \ + case 8: \ + *(ptr) = (val); \ + break; \ + default: \ + __build_bug_unaligned_length(); \ + break; \ + } \ + (void)0; }) + +static inline u16 get_unaligned_le16 (const void *p) +{ +return le16_to_cpup ((__le16 *)p); +} + +static inline u32 get_unaligned_le32 (const void *p) +{ +return le32_to_cpup ((__le32 *)p); +} + +static inline u64 get_unaligned_le64 (const void *p) +{ +return le64_to_cpup ((__le64 *)p); +} + +static inline u16 get_unaligned_be16 (const void *p) +{ +return be16_to_cpup ((__be16 *)p); +} + +static inline u32 get_unaligned_be32 (const void *p) +{ +return be32_to_cpup ((__be32 *)p); +} + +static inline u64 get_unaligned_be64 (const void *p) +{ +return be64_to_cpup ((__be64 *)p); +} + +static inline void put_unaligned_le16 (u16 val,void *p) +{ + *((__le16 *)p) =cpu_to_le16 (val); +} + +static inline void put_unaligned_le32 (u32 val,void *p) +{ + *((__le32 *)p) =cpu_to_le32 (val); +} + +static inline void put_unaligned_le64 (u64 val,void *p) +{ + *((__le64 *)p) =cpu_to_le64 (val); +} + +static inline void put_unaligned_be16 (u16 val,void *p) +{ + *((__be16 *)p) =cpu_to_be16 (val); +} + +static inline void put_unaligned_be32 (u32 val,void *p) +{ + *((__be32 *)p) =cpu_to_be32 (val); +} + +static inline void put_unaligned_be64 (u64 val,void *p) +{ + *((__be64 *)p) =cpu_to_be64 (val); +} + +#endif /* _LINUX_UNALIGNED_ACCESS_OK_H_ */
include/linux/unaligned/big_endian.h new file +86 -0
86 lines not on this page · raw message
include/linux/unaligned/cpu_endian.h new file +92 -0
92 lines not on this page · raw message
include/linux/unaligned/generic.h new file +67 -0
67 lines not on this page · raw message
include/linux/unaligned/generic_be.h new file +70 -0
70 lines not on this page · raw message
include/linux/unaligned/generic_le.h new file +70 -0
70 lines not on this page · raw message
include/linux/unaligned/little_endian.h new file +86 -0
86 lines not on this page · raw message
include/linux/unaligned/no_builtin_memcpy.h new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
1.5.5.144.g3e42
[Harvey Harrison - Thu, Apr 10, 2008 at 08:38:57PM -0700] [...]
| +
| +static inline void __put_unaligned_be16(u16 val, u8 *p)
| +{
| + *p++ = val >> 8;
| + *p++ = val;
| +}
| +
[...]
| +static inline void __put_unaligned_le16(u16 val, u8 *p)
| +{
| + *p++ = val;
| + *p++ = val >> 8;
| +}[...]
Hi Harvey, do we really need second increments? I mean, wouldn't be better to use *p = val >> 8 and *p << 8?
- Cyrill -