简体   繁体   English

4字节未对齐写入地址

[英]4 byte misaligned write to address

I'm doing a port of the CHibiOS RTOS to the lm32 microprocessor. 我正在将CHibiOS RTOS的端口连接到lm32微处理器。

I have a problem of misaligned memory address in the first line of code that I codified to setup a new thread. 我在编入代码以设置新线程的第一行代码中遇到内存地址未对齐的问题。 The other three lines already gave to me a similar problem when they tried to write to memory, but I solved it aligning the intctx and context struct putting __attribute__((packed)); 其他三行在尝试写入内存时已经给了我一个类似的问题,但是我解决了它,将intctxcontext结构对齐了__attribute__((packed)); .

The code is the following: 代码如下:

tp->p_ctx.sp = (struct intctx*)((uint32_t *)wsp + size - sizeof(struct intctx));
tp->p_ctx.sp->r1 = (uint32_t)arg;
tp->p_ctx.sp->r2 = (uint32_t)pf;
tp->p_ctx.sp->ra = (uint32_t)port_thread_start;

The structs are defined in a header file implemented by me: 这些结构在我实现的头文件中定义:

struct intctx {
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r4;
uint32_t r5;
uint32_t r6;
uint32_t r7;
uint32_t r8;
uint32_t r9;
uint32_t r10;
uint32_t r11;
uint32_t r12;
uint32_t r13;
uint32_t r14;
uint32_t r15;
uint32_t r16;
uint32_t r17;
uint32_t r18;
uint32_t r19;
uint32_t r20;
uint32_t r21;
uint32_t r22;
uint32_t r23;
uint32_t r24;
uint32_t r25;
uint32_t gp;
uint32_t fp;
uint32_t sp;
uint32_t ra;
uint32_t ea;
uint32_t ba;
} __attribute__((packed));

struct context {
struct intctx *sp;
} __attribute__((packed));

I used gdb to debug and when it try to execute the line: 我使用gdb进行调试,并尝试执行该行:

tp->p_ctx.sp = (struct intctx*)((uint32_t *)wsp + size - sizeof(struct intctx)); 

It gives the following problem: 它产生以下问题:

core: 4 byte misaligned write to address 0x107409 at 0x100b20

Program received signal SIGBUS, Bus error.
0x00000080 in ?? ()

Can anyone help me? 谁能帮我? Thank you. 谢谢。


The wsp is passed by reference as parameter of the function where these lines of code are. wsp通过引用作为这些代码所在的函数的参数传递。 the wsp has the type void *: But this is a Thread * type, wsp point to the idle thread struct. wsp的类型为void *:但这是Thread *类型,wsp指向空闲线程结构。

The line of code is implemented in the same function in other architectures that ChibiOS has support and I only did the same: 该代码行在ChibiOS支持的其他体系结构中以相同的功能实现,而我只是这样做:

tp->p_ctx.sp =  (struct intctx*)((uint32_t *)wsp + size - sizeof(struct intctx));

This is the complete function: 这是完整的功能:

Thread *chThdCreateI(void *wsp, size_t size,
                 tprio_t prio, tfunc_t pf, void *arg) {
/* Thread structure is laid out in the lower part of the thread workspace.*/
Thread *tp = wsp;

chDbgCheckClassI();

chDbgCheck((wsp != NULL) && (size >= THD_WA_SIZE(0)) &&
         (prio <= HIGHPRIO) && (pf != NULL),
         "chThdCreateI");
tp->p_ctx.sp =  (struct intctx*)((uint32_t *)wsp + size - sizeof(struct intctx));
tp->p_ctx.sp->r1 = (uint32_t)arg;
tp->p_ctx.sp->r2 = (uint32_t)pf;
tp->p_ctx.sp->ra = (uint32_t)port_thread_start;
//SETUP_CONTEXT(wsp, size, pf, arg);
return _thread_init(tp, prio);
}

What is the type of wsp? wsp是什么类型? I'd suggest that an array of chars, or whatever else you're defining it as isn't required to be suitably aligned to store int32_t types. 我建议使用一个char数组,或者您定义的其他任何数组,不需要适当对齐以存储int32_t类型。 Consider how buses are conventionally aligned to retrieve int32_t values aligned in groups of 32 bits. 考虑一下传统上如何对齐总线以检索以32位为一组对齐的int32_t值。 Now consider what "bus error" might actually mean on the architecture level: 现在考虑在体系结构级别上“总线错误”的实际含义:

  1. More than one fetch is required to retrieve the value (undesirable in terms of performance), or 要获取该值(在性能方面是不希望的),需要多次获取,或者
  2. Your program malfunctions (even worse) 您的程序出现故障(甚至更糟)

On common Intel implementations, it uses the first option unless you or your debugger inject some assembly madness to your program (for example, see this wikipedia article ). 在常见的Intel实现中,除非您或您的调试器对程序注入了某种疯狂的程序,否则它会使用第一个选项(例如,请参阅Wikipedia文章 )。 In C, it's just plain undefined behaviour. 在C语言中,这只是普通的未定义行为。 Make sure wsp is suitably aligned to point at int32_t types. 确保wsp正确对齐以指向int32_t类型。 You can do this by ensuring that it points at one of these: 您可以通过确保它指向以下其中一项来做到这一点:

  1. An int32_t variable, or 一个int32_t变量,或者
  2. Any int32_t objects within an array, or: 数组中的任何int32_t对象,或:
  3. A return value of malloc, calloc or realloc, or: malloc,calloc或realloc的返回值,或者:
  4. Any int32_t objects of "a malloc/calloc/realloc return value treated as a pointer to int32_t ". 任何“ malloc / calloc / realloc返回值的int32_t对象都被视为指向int32_t的指针”。

I think you're confused in regards to pointer arithmetic. 我认为您对指针算术感到困惑。 Which book are you reading? 您正在读哪本书?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM