can't find a register in class 'CREG'

2019-07-25 15:04发布

I am trying to make an earlier verion Linux got compiled, you can download the source code from git://github.com/azru0512/linux-0.12.git. While compiling ''kernel/blk_drv/ramdisk.c'', I got error message below,

ramdisk.c:36:10: error: can't find a register in class 'CREG' while reloading 'asm'
ramdisk.c:40:10: error: can't find a register in class 'CREG' while reloading 'asm'
ramdisk.c:36:10: error: 'asm' operand has impossible constraints
ramdisk.c:40:10: error: 'asm' operand has impossible constraints

What in ramdisk.c are,

  if (CURRENT-> cmd == WRITE) {
    (void) memcpy(addr,
            CURRENT->buffer,
            len);
  } else if (CURRENT->cmd == READ) {
    (void) memcpy(CURRENT->buffer,
            addr,
            len);
  } else
    panic("unknown ramdisk-command");

And the memcpy is,

extern inline void * memcpy(void * dest,const void * src, int n)
{
__asm__("cld\n\t"
  "rep\n\t"
  "movsb"
  ::"c" (n),"S" (src),"D" (dest)
  :"cx","si","di");
return dest;
}

I guess it's memcpy (include/string.h) inline asm problem, so I remove the clobber list from it but without luck. Could you help me to find out what's going wrong? Thanks!

2条回答
成全新的幸福
2楼-- · 2019-07-25 15:24

GCC's syntax for this has changed / evolved a bit.

You must now specify each of the special target registers as an output operand:

...("...instructions..."
   : "=c"(n), "=S"(src), "=D"(dest)

and then additionally as the same registers as source operands:

   : "0"(n), "1"(src), "2"(dest)

and finally you need to clobber "memory" (I can't remember offhand if this affects condition codes, if so you would also need "cc"):

   : "memory")

Next, because this instruction should not be moved or deleted, you need to use either volatile or __volatile__ (I'm not entirely sure why but without this the instructions were deleted, in my test-case).

Last, it's no longer a good idea to attempt to override memcpy because gcc "knows" how to implement the function. You can override gcc's knowledge with -fno-builtin.

This compiles (for me anyway, with a somewhat old gcc on an x86-64 machine):

extern inline void * memcpy(void * dest,const void * src, int n)
{
    __asm__ volatile("cld\n\t"
        "rep\n\tmovsb\n\t"
        : "=c" (n), "=S" (src), "=D" (dest)
        : "0" (n), "1" (src), "2" (dest)
        : "memory", "cc");
    return dest;
}
查看更多
欢心
3楼-- · 2019-07-25 15:24

This exact problem & its reasons are discussed on GCC's bugzilla :

Bug 43998 - inline assembler: can't set clobbering for input register

gcc wont allow input & output registers as clobbers. If you corrupt input register, do a dummy output to same register :

unsigned int operation;
unsigned int dummy;
asm ("cpuid" : "=a" (dummy) : "0" ( operation) :);
查看更多
登录 后发表回答