Author Topic: cr0 register and assembler in line (VS C++)  (Read 456 times)

nonosto

  • Regular Member
  • *
  • Posts: 26
cr0 register and assembler in line (VS C++)
« 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:

Code: [Select]
cr0 CXX0017: Error: symbol "cr0" not found

Visual C++ assembler in line code:


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

Code: [Select]
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

Code: [Select]
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;
}
« Last Edit: May 20, 2018, 04:03:30 AM by nonosto »

AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #1 on: May 19, 2018, 08:05:26 PM »
In Windowz, you need to be in ring 0 to play with cr0. OK?

nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #2 on: May 19, 2018, 08:23:54 PM »
I know. I work in kernel mode.

AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #3 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.

nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #4 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:

Code: [Select]
mov cr0,eax
Note that no error from compilator or warning.
« Last Edit: May 20, 2018, 05:27:31 PM by nonosto »

nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #5 on: May 20, 2018, 05:36:02 PM »
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:

Code: [Select]
void disableCache(void)
{
      __asm
   {    
mov     eax, cr0   
        or        eax, 0FFFFFFFH   
        mov     cr0, eax
   }
  return;
}

instead of:

Code: [Select]
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?

AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #6 on: May 20, 2018, 07:08:00 PM »
Like this:

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


nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #7 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:
Code: [Select]
    mov     eax, cr0
    bts     eax, 30
    btr     eax, 29
    mov     cr0, eax

AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #8 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.

AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #9 on: May 20, 2018, 09:51:59 PM »


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.

nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #10 on: May 21, 2018, 04:22:30 AM »
THX
freeze always same line. Watch give me this:

Code: [Select]
eax 3221291067 unsigned long
mask 1610612736 unsigned long


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

Code: [Select]
mov eax,cr0
bts     eax, 30
btr     eax, 29
mov cr0,eax

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

Code: [Select]
mov eax,cr0
bts     eax, 30
bts     eax, 29
mov cr0,eax

It's a Pentium III.
« Last Edit: May 21, 2018, 05:52:16 AM by nonosto »

nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #11 on: May 21, 2018, 06:42:40 AM »
this code dont freeze but I dont know if do the job.

Code: [Select]
    mov eax, cr0
    or  ax, 1
    mov cr0, eax

Someone advice me:

Code: [Select]
mov ecx, 0x2ff
rdmsr
mov al, 0x06
wrmsr

« Last Edit: May 21, 2018, 12:46:45 PM by nonosto »

AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #12 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.






nonosto

  • Regular Member
  • *
  • Posts: 26
Re: cr0 register and assembler in line (VS C++)
« Reply #13 on: May 21, 2018, 09:23:41 PM »
THX
If I try to remove all watch/snoop, but freeze what other must remove?


AW

  • Member
  • *****
  • Posts: 1346
  • Let's Make ASM Great Again!
Re: cr0 register and assembler in line (VS C++)
« Reply #14 on: May 21, 2018, 10:57:17 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.