minor fixes discovered during actual impl

This commit is contained in:
Hane 2023-07-21 21:31:21 +02:00
commit 64727ac279

View file

@ -57,6 +57,17 @@ bool checkInvariantTSC(){ //Generic
return iTSC; return iTSC;
} }
bool checkRDTSCP(){ //Generic
uint64_t rdx;
asm volatile (".intel_syntax noprefix\t\n" \
"mov eax, 0x80000001\t\n" \
// 080000007h\t\n"
"cpuid\t\n" : "=d" (rdx) );
bool RDTSCP = rdx & (1<<27);
//"and edx, 10000000\n")
return RDTSCP;
}
/* /*
1er comentario: https://community.intel.com/t5/Software-Tuning-Performance/TSC-frequency-computation/m-p/1193432 1er comentario: https://community.intel.com/t5/Software-Tuning-Performance/TSC-frequency-computation/m-p/1193432
@ -77,12 +88,13 @@ ECX Bits 31-00: An unsigned integer which is the nominal frequency of the core c
EDX Bits 31-00: Reserved = 0. EDX Bits 31-00: Reserved = 0.
*/ */
char* AMDMeCagoEnTusPutosMuertos () { char* AMDMeCagoEnTusPutosMuertos () {
/* /*
* Extended name on AMD systems when using 8000_002-4. 8000_0000 for Extpagequant/vendor. Hijos de puta. * Extended name on AMD systems when using 8000_002-4. 8000_0000 for Extpagequant/vendor. Hijos de puta.
* Now parsing 0H for standard brand name * Now parsing 0H for standard brand name
*/ */
char* palabritas = (char*)malloc(sizeof(char) * 12); char* palabritas = (char*)malloc(sizeof(char) * 13);
uint8_t siguienteLetra = 0; uint8_t siguienteLetra = 0;
//uint64_t regs[4]; //rax rbx, rcx, rdx; Was used for Extended name/vendor //uint64_t regs[4]; //rax rbx, rcx, rdx; Was used for Extended name/vendor
uint64_t regs[3];//rbx, rcx, rdx; uint64_t regs[3];//rbx, rcx, rdx;
@ -90,13 +102,13 @@ char* AMDMeCagoEnTusPutosMuertos () {
"mov eax, 0x0\t\n" \ "mov eax, 0x0\t\n" \
"cpuid\t\n" : "=b" (regs[0]), "=c" (regs[2]), "=d" (regs[1]) ); "cpuid\t\n" : "=b" (regs[0]), "=c" (regs[2]), "=d" (regs[1]) );
int byte = 0; int byte = 0;
for (int reg = 0; reg < 4; reg++, byte = 0){ for (int reg = 0; reg < 3; reg++, byte = 0){
char* charifiedReg = (char*)&regs[reg]; char* charifiedReg = (char*)&regs[reg];
for(; byte < 4; byte++, siguienteLetra++ ){ for(; byte < 4; byte++, siguienteLetra++ ){
palabritas[siguienteLetra] = charifiedReg[byte]; palabritas[siguienteLetra] = charifiedReg[byte];
} }
} }
palabritas[(siguienteLetra + 1)] = '\0'; palabritas[siguienteLetra] = '\0';
return palabritas; return palabritas;
} }
@ -110,12 +122,14 @@ bool AMDJoderMET(){
} }
int16_t checkFrequencyTSC(uint64_t* freq){ int16_t checkFrequencyTSC(uint64_t* freq){
//la idea era hacerla todo aqui; este primero mira el num de pags
if (freq == nullptr) return -1; if (freq == nullptr) return -1;
uint64_t raxde; uint64_t raxde;
asm volatile ( ".intel_syntax noprefix\t\n" \ asm volatile ( ".intel_syntax noprefix\t\n" \
"mov eax, 0x0\t\n" \ "mov eax, 0x0\t\n" \
"cpuid\t\n" : "=a" (raxde) ); "cpuid\t\n" : "=a" (raxde) );
log_debugcpp("raxo " << raxde);
if (raxde < 15) { if (raxde < 15) {
//check tsc existence?????? //check tsc existence??????
uint64_t rdxmsr; uint64_t rdxmsr;
@ -135,10 +149,12 @@ int16_t checkFrequencyTSC(uint64_t* freq){
"mov eax, 0x15\t\n" \ "mov eax, 0x15\t\n" \
"cpuid\t\n" : "=a" (raxde), "=b" (rbxnum), "=c" (rcxhz) ); "cpuid\t\n" : "=a" (raxde), "=b" (rbxnum), "=c" (rcxhz) );
//"rdtscp\n" : "=a" (raxlo), "=d" (rdxho)); //"rdtscp\n" : "=a" (raxlo), "=d" (rdxho));
log_debugcpp("hmm " << raxde << " " << rbxnum << " " << rcxhz);
if (rbxnum == 0 || rcxhz == 0) return -2; if (rbxnum == 0 || rcxhz == 0) return -2;
//also 18.17.4 Invariant Time-Keepin Volume 3 Intel //also 18.17.4 Invariant Time-Keepin Volume 3 Intel
uint64_t TSCFreq = (rcxhz * rbxnum)/raxde; uint64_t TSCFreq = (rcxhz * rbxnum)/raxde;
*freq = TSCFreq; *freq = TSCFreq;
log_debugcpp("raxo " << TSCFreq << " " << *freq);
return 0; return 0;
} }
@ -160,7 +176,7 @@ uint64_t rdtscp(uint64_t* logicore = nullptr ) {
"rdtscp\t\n" "rdtscp\t\n"
"lfence\t\n": "=a" (raxlo), "=d" (rdxho), "=c" (rcx) ); "lfence\t\n": "=a" (raxlo), "=d" (rdxho), "=c" (rcx) );
//"rdtscp\n" : "=a" (raxlo), "=d" (rdxho)); //"rdtscp\n" : "=a" (raxlo), "=d" (rdxho));
if (logicore == nullptr) *logicore = rcx; if (logicore != nullptr) *logicore = rcx;
return (rdxho << 32) + raxlo; //| return (rdxho << 32) + raxlo; //|
} }
@ -214,7 +230,10 @@ uint64_t estimandoFreq(){
&longestDataSize, // longest value data &longestDataSize, // longest value data
NULL, // security descriptor NULL, // security descriptor
NULL); // last write time NULL); // last write time
if(status != ERROR_SUCCESS) return -2; if(status != ERROR_SUCCESS) {
RegCloseKey(key);
return -2;
}
dataLen = sizeof(REG_QWORD) * (longestDataSize); dataLen = sizeof(REG_QWORD) * (longestDataSize);
dataContent = (LPBYTE)malloc(dataLen); dataContent = (LPBYTE)malloc(dataLen);
@ -234,7 +253,10 @@ uint64_t estimandoFreq(){
&dataLen &dataLen
); );
if(status != ERROR_SUCCESS) return -3; if(status != ERROR_SUCCESS) {
RegCloseKey(key);
return -3
}
if (CSTR_EQUAL == CompareStringEx( if (CSTR_EQUAL == CompareStringEx(
LOCALE_NAME_INVARIANT, LOCALE_NAME_INVARIANT,
@ -265,13 +287,17 @@ uint64_t estimandoFreq(){
LPDWORD PMHz = (LPDWORD)dataContent; LPDWORD PMHz = (LPDWORD)dataContent;
DWORD MHz = *PMHz; DWORD MHz = *PMHz;
free(dataContent); free(dataContent);
RegCloseKey(key);
return MHz * 1000000; return MHz * 1000000;
//break; //break;
} }
} }
return -4; if(status != ERROR_SUCCESS) {
RegCloseKey(key);
return -4
}
} }
#endif #endif
@ -285,15 +311,18 @@ int main(int argc, char** argv){
uint64_t isItActually32GHz = estimandoFreq(); uint64_t isItActually32GHz = estimandoFreq();
log_debugcpp("ayo we found " << isItActually32GHz << " flash knuckle"); log_debugcpp("ayo we found " << isItActually32GHz << " flash knuckle");
} }
int64_t juas = getFreqFromReg(); int64_t juas = getFreqFromReg();
log_debugcpp("JODER " << juas); log_debugcpp("JODER " << juas);
//iwintewel
//Cordura //Cordura
int16_t result; int16_t result;
uint64_t tf = 0; uint64_t tf = 0;
uint64_t* TSCFreq = &tf; //uint64_t* TSCFreq = &tf;
bool iTSC = checkInvariantTSC(); bool iTSC = checkInvariantTSC();
result = checkFrequencyTSC(TSCFreq); bool RDTSC = checkRDTSCP();
result = checkFrequencyTSC(&tf);
log_debugcpp("INTEL YO HOW WE DOIN " << tf << result);
if (iTSC && !result) { if (iTSC && !result) {
uint64_t test = rdtsc(); uint64_t test = rdtsc();
log_debugcpp(test); log_debugcpp(test);