Tue, 28 Dec 2010 19:10:36 +0000
fix UDS/LDS in bus error handling for 16/32-bit accesses
1 #ifndef _MEMORY_H
2 #define _MEMORY_H
4 /***********************************
5 * Array read/write utility macros
6 * "Don't Repeat Yourself" :)
7 ***********************************/
9 /// Array read, 32-bit
10 #define RD32(array, address, andmask) \
11 (((uint32_t)array[(address + 0) & (andmask)] << 24) | \
12 ((uint32_t)array[(address + 1) & (andmask)] << 16) | \
13 ((uint32_t)array[(address + 2) & (andmask)] << 8) | \
14 ((uint32_t)array[(address + 3) & (andmask)]))
16 /// Array read, 16-bit
17 #define RD16(array, address, andmask) \
18 (((uint32_t)array[(address + 0) & (andmask)] << 8) | \
19 ((uint32_t)array[(address + 1) & (andmask)]))
21 /// Array read, 8-bit
22 #define RD8(array, address, andmask) \
23 ((uint32_t)array[(address + 0) & (andmask)])
25 /// Array write, 32-bit
26 #define WR32(array, address, andmask, value) { \
27 array[(address + 0) & (andmask)] = (value >> 24) & 0xff; \
28 array[(address + 1) & (andmask)] = (value >> 16) & 0xff; \
29 array[(address + 2) & (andmask)] = (value >> 8) & 0xff; \
30 array[(address + 3) & (andmask)] = value & 0xff; \
31 }
33 /// Array write, 16-bit
34 #define WR16(array, address, andmask, value) { \
35 array[(address + 0) & (andmask)] = (value >> 8) & 0xff; \
36 array[(address + 1) & (andmask)] = value & 0xff; \
37 }
39 /// Array write, 8-bit
40 #define WR8(array, address, andmask, value) \
41 array[(address + 0) & (andmask)] = value & 0xff;
43 /******************
44 * Memory mapping
45 ******************/
47 typedef enum {
48 MEM_ALLOWED = 0,
49 MEM_PAGEFAULT, // Page fault -- page not present
50 MEM_PAGE_NO_WE, // Page not write enabled
51 MEM_KERNEL, // User attempted to access kernel memory
52 MEM_UIE // User Nonmemory Location Access
53 } MEM_STATUS;
55 /**
56 * @brief Check memory access permissions for a given address.
57 * @param addr Address.
58 * @param writing true if writing to memory, false if reading.
59 * @return One of the MEM_STATUS constants, specifying whether the access is
60 * permitted, or what error occurred.
61 */
62 MEM_STATUS checkMemoryAccess(uint32_t addr, bool writing);
64 /**
65 * @brief Map a CPU memory address into physical memory space.
66 * @param addr Address.
67 * @param writing true if writing to memory, false if reading.
68 * @return Address, remapped into physical memory.
69 */
70 uint32_t mapAddr(uint32_t addr, bool writing);
72 #endif