How about this patch. this also works.

Is it acceptable ?


> On Dec 2, 2008, at 12:46 PM, Hiroshi Ito wrote:
> 
> > I'm running openocd on EP9307(arm925t) CPU, as a HOST.
> > and target is EP9307 and ARM926t with EP9307 GPIO.
> >
> > revision 1183 cause seg fault.
> > The problem is, cmd_queue_alloc returns unaligned pointer.
> > but it is used as a pointer to structure.
> >
> > This patch fix it. and it is working.
> > Index: src/jtag/jtag.c
> > ===================================================================
> > --- src/jtag/jtag.c (revision 1183)
> > +++ src/jtag/jtag.c (working copy)
> > @@ -378,6 +378,8 @@
> >     int offset;
> >     u8 *t;
> >
> > +   /* align to 4 byte boundary */
> > +   size = ((size+3)/4)*4;
> >     if (*p_page)
> >     {
> >             while ((*p_page)->next)
> > _______________________________________________
> > Openocd-development mailing list
> > Openocd-development@lists.berlios.de
> > https://lists.berlios.de/mailman/listinfo/openocd-development
> 
> 
> 
> This doesn't change the pointer returned by cmd_queue_alloc() at all.   
> It just rounds the size of the allocation up to a 4-byte alignment.   
> If that is necessary, it should be fixed by the caller rather than  
> always padding in cmd_queue_alloc().  Otherwise, a 1-byte allocation  
> would take 4-bytes in all cases.  What function was calling  
> cmd_queue_alloc() that needs the pointer to be word aligned?
> 
> I'm also not entirely sure that a structure must always be aligned.   
> I'm aware that some architectures will trap if an unaligned access is  
> encountered, but the OS should catch the trap and handle the unaligned  
> access correctly.
> 
> --
> Rick Altherr
> [EMAIL PROTECTED]
> 
> "He said he hadn't had a byte in three days. I had a short, so I split  
> it with him."
>   -- Unsigned

--------
Hiroshi Ito
Media Lab. Inc.,
URL http://www.mlb.co.jp ( Sorry, Japanese only. )
TEL +81-3-5294-7255  FAX +81-3-5294-7256

Index: jtag/jtag.c
===================================================================
--- jtag/jtag.c (revision 1183)
+++ jtag/jtag.c (working copy)
@@ -372,17 +372,24 @@
        return NULL;
 }
 
-void* cmd_queue_alloc(size_t size)
+
+/* align must be power of 2 and non-zero */
+void* cmd_queue_alloc_aligned(size_t size, size_t align)
 {
        cmd_queue_page_t **p_page = &cmd_queue_pages;
        int offset;
+       int padding = 0;
        u8 *t;
 
        if (*p_page)
        {
                while ((*p_page)->next)
                        p_page = &((*p_page)->next);
-               if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
+
+               padding = (*p_page)->used & (align-1);
+               if ( padding ) padding = align - padding;
+
+               if (CMD_QUEUE_PAGE_SIZE - ((*p_page)->used + padding) < size)
                        p_page = &((*p_page)->next);
        }
 
@@ -392,8 +399,10 @@
                (*p_page)->used = 0;
                (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
                (*p_page)->next = NULL;
+               padding = 0;
        }
 
+       (*p_page)->used += padding;
        offset = (*p_page)->used;
        (*p_page)->used += size;
 
@@ -401,6 +410,19 @@
        return t + offset;
 }
 
+void* cmd_queue_alloc(size_t size)
+{
+    return cmd_queue_alloc_aligned(size, 1);
+}
+void* cmd_queue_alloc_pointer(size_t size)
+{
+    return cmd_queue_alloc_aligned(size, sizeof(void *));
+}
+void* cmd_queue_alloc_int(size_t size)
+{
+    return cmd_queue_alloc_aligned(size, sizeof(int));
+}
+
 void cmd_queue_free(void)
 {
        cmd_queue_page_t *page = cmd_queue_pages;
@@ -461,16 +483,16 @@
        last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        (*last_cmd)->next = NULL;
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->type = JTAG_SCAN;
 
        /* allocate memory for ir scan command */
-       (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
+       (*last_cmd)->cmd.scan = cmd_queue_alloc_pointer(sizeof(scan_command_t));
        (*last_cmd)->cmd.scan->ir_scan = 1;
        (*last_cmd)->cmd.scan->num_fields = jtag_num_devices;   /* one field 
per device */
-       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * 
sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->fields = 
cmd_queue_alloc_pointer(jtag_num_devices * sizeof(scan_field_t) );
        (*last_cmd)->cmd.scan->end_state = state;
 
        for (i = 0; i < jtag_num_devices; i++)
@@ -550,16 +572,16 @@
        last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        (*last_cmd)->next = NULL;
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->type = JTAG_SCAN;
 
        /* allocate memory for ir scan command */
-       (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
+       (*last_cmd)->cmd.scan = cmd_queue_alloc_pointer(sizeof(scan_command_t));
        (*last_cmd)->cmd.scan->ir_scan = 1;
        (*last_cmd)->cmd.scan->num_fields = num_fields;
-       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * 
sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc_pointer(num_fields * 
sizeof(scan_field_t));
        (*last_cmd)->cmd.scan->end_state = state;
 
        for (i = 0; i < num_fields; i++)
@@ -614,16 +636,16 @@
        }
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->next = NULL;
        (*last_cmd)->type = JTAG_SCAN;
 
        /* allocate memory for dr scan command */
-       (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
+       (*last_cmd)->cmd.scan = cmd_queue_alloc_pointer(sizeof(scan_command_t));
        (*last_cmd)->cmd.scan->ir_scan = 0;
        (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
-       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + 
bypass_devices) * sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc_pointer((num_fields + 
bypass_devices) * sizeof(scan_field_t));
        (*last_cmd)->cmd.scan->end_state = state;
 
        for (i = 0; i < jtag_num_devices; i++)
@@ -704,16 +726,16 @@
        }
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->next = NULL;
        (*last_cmd)->type = JTAG_SCAN;
 
        /* allocate memory for dr scan command */
-       (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
+       (*last_cmd)->cmd.scan = cmd_queue_alloc_pointer(sizeof(scan_command_t) 
);
        (*last_cmd)->cmd.scan->ir_scan = 0;
        (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
-       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + 
bypass_devices) * sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc_pointer((num_fields + 
bypass_devices) * sizeof(scan_field_t) );
        (*last_cmd)->cmd.scan->end_state = end_state;
 
        for (i = 0; i < jtag_num_devices; i++)
@@ -785,16 +807,16 @@
        jtag_command_t **last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->next = NULL;
        (*last_cmd)->type = JTAG_SCAN;
 
        /* allocate memory for scan command */
-       (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
+       (*last_cmd)->cmd.scan = cmd_queue_alloc_pointer(sizeof(scan_command_t));
        (*last_cmd)->cmd.scan->ir_scan = 0;
        (*last_cmd)->cmd.scan->num_fields = num_fields;
-       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * 
sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc_pointer(num_fields * 
sizeof(scan_field_t));
        (*last_cmd)->cmd.scan->end_state = state;
 
        for (i = 0; i < num_fields; i++)
@@ -831,12 +853,12 @@
        jtag_command_t **last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->next = NULL;
        (*last_cmd)->type = JTAG_STATEMOVE;
 
-       (*last_cmd)->cmd.statemove = 
cmd_queue_alloc(sizeof(statemove_command_t));
+       (*last_cmd)->cmd.statemove = 
cmd_queue_alloc_aligned(sizeof(statemove_command_t), sizeof(enum tap_state) );
        (*last_cmd)->cmd.statemove->end_state = state;
 
 
@@ -887,14 +909,14 @@
        int i;
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->next = NULL;
        (*last_cmd)->type = JTAG_PATHMOVE;
 
-       (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
+       (*last_cmd)->cmd.pathmove = 
cmd_queue_alloc_pointer(sizeof(pathmove_command_t));
        (*last_cmd)->cmd.pathmove->num_states = num_states;
-       (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum 
tap_state) * num_states);
+       (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc_aligned(sizeof(enum 
tap_state) * num_states, sizeof(enum tap_state));
 
        for (i = 0; i < num_states; i++)
                (*last_cmd)->cmd.pathmove->path[i] = path[i];
@@ -907,12 +929,12 @@
        jtag_command_t **last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        (*last_cmd)->next = NULL;
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->type = JTAG_RUNTEST;
 
-       (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
+       (*last_cmd)->cmd.runtest = 
cmd_queue_alloc_int(sizeof(runtest_command_t));
        (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
        (*last_cmd)->cmd.runtest->end_state = state;
 
@@ -1039,12 +1061,12 @@
        jtag_command_t **last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        (*last_cmd)->next = NULL;
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->type = JTAG_RESET;
 
-       (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
+       (*last_cmd)->cmd.reset = cmd_queue_alloc_int(sizeof(reset_command_t));
        (*last_cmd)->cmd.reset->trst = req_trst;
        (*last_cmd)->cmd.reset->srst = req_srst;
 
@@ -1065,12 +1087,12 @@
        jtag_command_t **last_cmd = jtag_get_last_command_p();
 
        /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       *last_cmd = cmd_queue_alloc_pointer(sizeof(jtag_command_t));
        (*last_cmd)->next = NULL;
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->type = JTAG_SLEEP;
 
-       (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
+       (*last_cmd)->cmd.sleep = cmd_queue_alloc_int(sizeof(sleep_command_t));
        (*last_cmd)->cmd.sleep->us = us;
 
        return ERROR_OK;
_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to