News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

cr0 register and assembler in line (VS C++)

Started by nonosto, May 19, 2018, 04:57:54 PM

Previous topic - Next topic

nonosto

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;
}

aw27

In Windowz, you need to be in ring 0 to play with cr0. OK?

nonosto


aw27

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

#4
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.

nonosto

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?

aw27

Like this:

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


nonosto

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

aw27

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.

aw27



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

#10
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.

nonosto

#11
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



aw27

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

THX
If I try to remove all watch/snoop, but freeze what other must remove?


aw27

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.