Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* problem with unaligned access on Alpha
@ 1999-06-22 10:59 Christian Groessler
  1999-06-22 12:32 ` Stefan Katzenbeisser
  0 siblings, 1 reply; 14+ messages in thread
From: Christian Groessler @ 1999-06-22 10:59 UTC (permalink / raw)
  To: egcs, axp-list




Hello,

while fixing a (intel-) program to work on Alpha, I've encountered
something,
I think it's a compiler bug.

Consider the following example program:

-----------------------
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>

#define READ_WORD(a)          (*(unsigned short *)(a))

int main(void)
{
  char * sepp,* xadr;
  int fd;
  int i;
  unsigned short val;

  fd=open("/dev/zero",O_RDWR);
  if (fd == -1) {
  xxx:
    fprintf(stderr,"cannot open : %s\n",strerror(errno));
    return(99);
  }
  sepp = mmap(0,0x10000,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
  if (sepp == -1) goto xxx;
  printf("mapped at address %p\n",sepp);

  xadr = sepp + 0x7bf;
  for (i=0; i< 0x7d0; i++) *(sepp + i) = 0xcc;

  printf("OK, now accessing %p....\n",xadr);
  val = READ_WORD(xadr);

  printf("val read = %04x\n",val);
  return(1);
}
-----------------------

I would expect the "val=READ_WORD()" line to generate an unaligned access
exception,
but this doesn't happen. Instead, only one _byte_ at address xadr is read,
resulting
in val=0xcc. Val is 0xcccc on intel machines.

I looked at the generated assembler code (with my little only alpha
assembler knowledge),
and it seems that the compiler is generating code for unaligned access but
somehow
forgets to get the byte at (adress+1).

This was reproduced on RH Linux 5.2 with egcs-1.0.3 and NetBSD 1.4C with
egcs-1.1.2.

Any comments?

regards,
chris



^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha
@ 1999-06-23 10:23 Christian Groessler
  1999-06-23 14:55 ` Maciej W. Rozycki
  1999-06-23 14:56 ` Michal Jaegermann
  0 siblings, 2 replies; 14+ messages in thread
From: Christian Groessler @ 1999-06-23 10:23 UTC (permalink / raw)
  To: axp-list; +Cc: Stefan Katzenbeisser



Hello Stefan,

On 6/22/99 1:32:39 PM Stefan Katzenbeisser  wrote:
>
>That's definitively a compiler bug. I solved the problem by upgrading
>to egcs 1.1.2

But it also happens with egcs-1.1.2 (on NetBSD, but I don't think this
makes a difference).

OK, now that I know whats the problem I can avoid the unaligned access and
fix the program. But I initially relied on unaligned accesses being
signalled
by the kernel, and now that the compiler works around unaligned accesses
(in a buggy way) makes it much more difficult to get alignment-unaware
(intel)
apps running on Alpha....

regards,
chris



^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha
@ 1999-06-24  8:29 Christian Groessler
  1999-06-24 15:26 ` Michal Jaegermann
  0 siblings, 1 reply; 14+ messages in thread
From: Christian Groessler @ 1999-06-24  8:29 UTC (permalink / raw)
  To: axp-list



On 6/23/99 3:56:17 PM Michal Jaegermann  wrote:
>
>Try to see if -fno-strict-aliasing compiler flag will make any difference.
>Likely it should.

Makes no difference.
But my problem isn't "how to fix it" but "why does the compiler silently
generate
garbage code" ....

regards,
chris



^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha
@ 1999-06-24  8:54 Christian Groessler
  0 siblings, 0 replies; 14+ messages in thread
From: Christian Groessler @ 1999-06-24  8:54 UTC (permalink / raw)
  To: Richard Henderson; +Cc: egcs, axp-list



On 6/23/99 6:35:21 PM Richard Henderson  wrote:
>
>On Tue, Jun 22, 1999 at 11:59:46AM +0100, Christian Groessler wrote:
>> #define READ_WORD(a)          (*(unsigned short *)(a))
>...
>> I would expect the "val=READ_WORD()" line to generate an unaligned
access
>> exception, but this doesn't happen.
>
>You expect incorrectly.
>
>In general you'll only get an unaligned access trap if we'd issued
>an `ldwu' instruction, which we can only do when compiling for EV56
>or higher.

I assume for shorts only. If I replace "unsigned short *" with "unsigned
int *"
I do get unaligned traps.


>> I looked at the generated assembler code (with my little only alpha
>> assembler knowledge), and it seems that the compiler is generating
>> code for unaligned access but somehow forgets to get the byte at
(adress+1).
>
>Nope, we're generating code for an _aligned_ word access in
>the only way that is possible on EV4.  An unaligned word
>access doubles the length of the sequence.

Now I see. The code for aligned word access breaks when the
word is unaligned at address+7, if I understand the assembly code
correctly. My problem is/was, that the code breaks _silently_, but
maybe this case is hard to detect (to give a warning etc.).

Thanks for the info.

regards,
chris



^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: problem with unaligned access on Alpha
@ 1999-06-29  9:12 Wessels, Hans
  1999-06-29 15:07 ` Michal Jaegermann
  0 siblings, 1 reply; 14+ messages in thread
From: Wessels, Hans @ 1999-06-29  9:12 UTC (permalink / raw)
  To: 'axp-list@redhat.com'

On 25 juni 1999 Maciej W. Rozycki wrote:

<snip>
>It may even be completely ignored for archs that support unaligned
>accesses in hardware, which is a true win -- no arch dependent
>conditionals are needed (I do not even know how to fetch information about
>alignment needed for a particular target -- "alignof" is useless here and
>it's not portable, anyway). 
<snip>
>
> Using unions is good when you are sure all data is aligned.  If that's
>not the case, the problem remains.  The "packed" approach is reasonable
>but not necessarily the best.  As C is relatively low-level language (some
>people call it "a portable assembler") I wouldn't mind if some way of
>alignment hinting became standard one day.

I am using the following macro to get alignment of elements in an
malloced memory block:

/*
 * ALIGN is a macro to align elements of a certain size in a malloced
 * block. The start of the block is guaranteed aligned with every
possible 
 * object. So an object is aligned when it's position relative to the
start
 * of the array is a multiple of it's size. Instead of the modulo
operator 
 * the & operator is used. This assumes that the alignment of an object 
 * is the smalest power of two of its size; ie: sizeof(object)==3 ->
alignment
 * is 1. sizeof(object)==6 ->alignment is 2.
 */
#define ALIGN(start,ptr,size) ptr+=((size)-((ptr)-(start)))&((size)-1)

some code....
{
      ...
      uint8 *base = malloc(memneed);

      if (base == NULL)
      {
        return GUP_OUT_OF_MEM;
      }
      else
      {
        uint8 *cp = base;

        com->dictionary = (void *)(cp + 4);  /* vier extra om terug te
kunnen kijken */
        cp += (DIC_SIZE + MAX_MATCH*4 + 6UL + 4UL) * sizeof (uint8); /*
sliding dictionary + 4 voor terugkijken */
        ALIGN(base, cp, sizeof(node_type)); /* allign buffer on
node_type allignment */
        com->root.big = (void *)cp;               /* 
        cp += sizeof (node_type) * HASH_SIZE; /* normal root */
        ALIGN(base, cp, sizeof(unsigned long)); /* align on unsigned
long for output buffer */
        com->buffer_start=(void*)cp;
        cp += com->buffer_size;
        ALIGN(base, cp, sizeof(node_struct));
        com->tree.big = (void *)cp;
        cp += sizeof (node_struct) * TREE_SIZE;  /* sld tree          */
        ALIGN(base, cp, sizeof(uint8));
        com->charlen = (void *)cp;
        cp += sizeof (uint8) * (NC + NC);  /* karakter lengte */
        ALIGN(base, cp, sizeof(uint16));
        com->ptr2huffman = (void *)cp;
        cp += sizeof (uint16) * (MAX_NPT); /* huffman codes van de
pointers */
        ALIGN(base, cp, sizeof(pointer_type));
        com->pointers = (void *)(cp);
        cp += buf * sizeof (pointer_type);
        ...
}

This code works fine from MC68K and a compiler using 68bit int's, going
through GCC on MC68K and i386 to GCC on the Alpha. I know it isn't
perfect but it works fine for me.

Bye, Hans
----------------------------------------------------------------------
   /|   /\/\/\   |\      ir. Hans Wessels
  / |  / /\/\ \  | \     H.Wessels@Energie-Delfland.nl
 /  | / / /\ \ \ |  \    tel.: 015 251 4397
/   |/ / /  \ \ \|   \   
\   |\ \ \  / / /|   /   prive:
 \  | \ \ \/ / / |  /    mr_ni@mbh.org
  \ |  \ \/\/ /  | /     Fidelus Bittus et Fuckus Clockcyclus!
   \|   \/\/\/   |/      


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~1999-07-08 18:33 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-06-22 10:59 problem with unaligned access on Alpha Christian Groessler
1999-06-22 12:32 ` Stefan Katzenbeisser
  -- strict thread matches above, loose matches on Subject: below --
1999-06-23 10:23 Christian Groessler
1999-06-23 14:55 ` Maciej W. Rozycki
1999-06-23 14:56 ` Michal Jaegermann
1999-06-23 16:23   ` Maciej W. Rozycki
1999-06-24  7:02     ` Michal Jaegermann
1999-06-25 20:21       ` Maciej W. Rozycki
1999-06-24  8:29 Christian Groessler
1999-06-24 15:26 ` Michal Jaegermann
1999-06-24  8:54 Christian Groessler
1999-06-29  9:12 Wessels, Hans
1999-06-29 15:07 ` Michal Jaegermann
1999-07-05 17:23   ` Maciej W. Rozycki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox