The MASM Forum

General => The Workshop => Topic started by: nonosto on May 19, 2018, 04:57:54 PM

Title: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 19, 2018, 04:57:54 PM
Hello world

You helped me to translate GCC assembler to Assembler in line for Visual C++. Compil good but when I debug apps freeze when it meet cr0 register. With watch I have value for all register except cr0.

I have this error:


cr0 CXX0017: Error: symbol "cr0" not found


Visual C++ assembler in line code:



void disableCache(void)
{
   uint32_t mask = (1 << 30) | (1 << 29);
   __asm
   {
      mov ecx,mask
      mov eax,cr0
      or eax,ecx
      mov cr0,eax
   }
  return;
}



void enableCache(void)
{
uint32_t mask = ~((1 << 30) | (1 << 29)); // CD | NW
  __asm
   {
      mov ecx,mask
      mov eax,cr0
      and eax,ecx
      mov cr0,eax
   }
  return;
}


Could you help me please?

THX

NB: GCC original code


void disableCache(void) {
  uint32_t mask = (1 << 30) | (1 << 29); // CD | NW
  __asm__ __volatile__ ("movl %%cr0,%%eax\n"
                        "orl %%ecx,%%eax\n"
                        "movl %%eax,%%cr0"
                        :
                        :"c"(mask)
                        :"eax","memory");
  return;
}

void enableCache(void) {
  uint32_t mask = ~((1 << 30) | (1 << 29)); // CD | NW
  __asm__ __volatile__ ("movl %%cr0,%%eax\n"
                        "andl %%ecx,%%eax\n"
                        "movl %%eax,%%cr0"
                        :
                        :"c"(mask)
                        :"eax","memory");
  return;
}
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 19, 2018, 08:05:26 PM
In Windowz, you need to be in ring 0 to play with cr0. OK?
Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 19, 2018, 08:23:54 PM
I know. I work in kernel mode.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 20, 2018, 03:28:55 AM
You did not mention it and most linux people that come here don't know it.
Anyway, your code looks good and is expected to work. Just a bit redundant, for example:
mov eax,cr0
or  eax,60000000h
mov cr0,eax

is enough for cache disable on 1 core.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 20, 2018, 01:49:01 PM
THX

my apps freeze at this line always, for any functions, and I have never value for CR0:

mov cr0,eax

Note that no error from compilator or warning.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 20, 2018, 05:36:02 PM
Quote from: aw27 on May 20, 2018, 03:28:55 AM
You did not mention it and most linux people that come here don't know it.
Anyway, your code looks good and is expected to work. Just a bit redundant, for example:
mov eax,cr0
or  eax,60000000h
mov cr0,eax

is enough for cache disable on 1 core.

If I understand I can use it instead of my first code. My configuration is very old 128mo de RAM, and when I use this code it doenot freeze:


void disableCache(void)
{
      __asm
   {    
mov     eax, cr0   
        or        eax, 0FFFFFFFH   
        mov     cr0, eax
   }
  return;
}


instead of:

void disableCache(void)
{
   uint32_t mask = (1 << 30) | (1 << 29);
   __asm
   {
      mov ecx,mask
      mov eax,cr0
      or eax,ecx
      mov cr0,eax
   }
  return;
}


Do you think is it the same? So Can do same think for the second function?
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 20, 2018, 07:08:00 PM
Like this:

void disableCache(void)
{
    __asm
   {
      mov eax,cr0
      or  eax,60000000h
      mov cr0,eax
   }
  return;
}

Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 20, 2018, 07:20:42 PM
THX
I already test it and freeze at the last line.
I found this, dont freeze but I dont know if it's the same:

    mov     eax, cr0
    bts     eax, 30
    btr     eax, 29
    mov     cr0, eax
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 20, 2018, 08:31:31 PM
Quote
My configuration is very old 128mo de RAM
I don't understand, can you be specific and tell what CPU and OS are you using.

Quote
mov     eax, cr0
    bts     eax, 30
    btr     eax, 29
    mov     cr0, eax
It is not the same thing.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 20, 2018, 09:51:59 PM
(http://www.atelierweb.com/a/cacheenabledisable.png)

The code runs well, the cache is disabled and enabled, cr0 changes as expected as you can see and Windbg debugs well. So, it is some issue you have to find out, probably not related to Visual Studio because it uses the Windbg engine.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 21, 2018, 04:22:30 AM
THX
freeze always same line. Watch give me this:


eax 3221291067 unsigned long
mask 1610612736 unsigned long



So when I move 30 to 1 and 29 to 0 no freeze:


mov eax,cr0
bts     eax, 30
btr     eax, 29
mov cr0,eax


but when I try it freeze...the problem seem bit 29:


mov eax,cr0
bts     eax, 30
bts     eax, 29
mov cr0,eax


It's a Pentium III.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 21, 2018, 06:42:40 AM
this code dont freeze but I dont know if do the job.


    mov eax, cr0
    or  ax, 1
    mov cr0, eax


Someone advice me:

mov ecx, 0x2ff
rdmsr
mov al, 0x06
wrmsr


Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 21, 2018, 09:04:36 PM
cr0 supports many different features but only the CD and NW flags have to do with caching. In other words, don't waste time experimenting with other bits.

For the Pentium III, when CD and NW bits are set, external snoops are supported only on dual processors, so it will hang in uniprocessor systems as you could confirm. If you don't snoop it will run good.





Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 21, 2018, 09:23:41 PM
THX
If I try to remove all watch/snoop, but freeze what other must remove?

(https://i62.servimg.com/u/f62/15/96/88/01/sans_t11.png)
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 21, 2018, 10:57:17 PM
Quote from: nonosto on May 21, 2018, 09:23:41 PM
THX
If I try to remove all watch/snoop, but freeze what other must remove?
If it does, you have to try other possibilities yourself because I have no Pentium III around here.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: nonosto on May 25, 2018, 10:14:02 PM
THX AW.
Quote from: AW on May 21, 2018, 09:04:36 PM
For the Pentium III, when CD and NW bits are set, external snoops are supported only on dual processors, so it will hang in uniprocessor systems as you could confirm.


In debug mode freeze but when I run the program no issue. I assume that It's seem impossible to run it in debug.
Title: Re: cr0 register and assembler in line (VS C++)
Post by: aw27 on May 26, 2018, 01:38:54 AM
That's what I thought.