From ed8081022b8adbcffb18fd2129d5c0f6f278604d Mon Sep 17 00:00:00 2001 From: volvet Date: Thu, 9 Jan 2014 12:18:30 +0800 Subject: [PATCH 1/5] fix cpu core query via cpuid --- codec/common/cpu.cpp | 19 ++++++++++++++----- codec/common/cpuid.asm | 5 ++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/codec/common/cpu.cpp b/codec/common/cpu.cpp index 5ad5c18f..9efab5a9 100644 --- a/codec/common/cpu.cpp +++ b/codec/common/cpu.cpp @@ -90,7 +90,8 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { /* CMOV instruction checking */ uiCPU |= WELS_CPU_CMOV; } - if (!strcmp ((const str_t*)chVenderName, CPU_Vender_INTEL)) { // confirmed_safe_unsafe_usage + if ((!strcmp ((const str_t*)chVenderName, CPU_Vender_INTEL)) || + (!strcmp((const str_t*)chVenderName, CPU_Vender_AMD)) ) { // confirmed_safe_unsafe_usage if (uiFeatureD & 0x10000000) { /* Multi-Threading checking: contains of multiple logic processors */ uiCPU |= WELS_CPU_HTT; @@ -130,10 +131,18 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { uiCPU |= WELS_CPU_MOVBE; } - if (pNumberOfLogicProcessors != NULL) { - // HTT enabled on chip - *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX - } + if( pNumberOfLogicProcessors != NULL ){ + if (!strcmp((const str_t*)chVenderName, CPU_Vender_AMD)){ + *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX + } else if( !strcmp((const str_t*)chVenderName, CPU_Vender_INTEL) ){ + uiFeatureC = 0; + WelsCPUId(0x4, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD); + *pNumberOfLogicProcessors = ((uiFeatureA&0xfc000000)>>26) + 1; + } else { + //FIXME: other cpus + *pNumberOfLogicProcessors = 1; + } + } WelsCPUId (0x80000000, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD); diff --git a/codec/common/cpuid.asm b/codec/common/cpuid.asm index f974dc90..2d19212e 100644 --- a/codec/common/cpuid.asm +++ b/codec/common/cpuid.asm @@ -85,6 +85,7 @@ WelsCPUId: push rdx mov eax, ecx + mov ecx, r9d cpuid mov [r9], ecx mov [r8], ebx @@ -105,7 +106,7 @@ WelsCPUId: mov eax, edi cpuid mov [r8], edx - pop rdx + pop rdx pop r8 mov [r8], ecx mov [rdx], ebx @@ -121,6 +122,8 @@ WelsCPUId: push edi mov eax, [esp+12] ; operating index + mov edi, [esp+24] + mov ecx, [edi] cpuid ; cpuid ; processing various information return From 02e4bf6e9d9d836ff817b0e33fd6bf6b476ce738 Mon Sep 17 00:00:00 2001 From: volvet Date: Thu, 9 Jan 2014 12:31:13 +0800 Subject: [PATCH 2/5] fix UNIX64 and WIN64: use ecx as input param --- codec/common/cpuid.asm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codec/common/cpuid.asm b/codec/common/cpuid.asm index 2d19212e..45b3784e 100644 --- a/codec/common/cpuid.asm +++ b/codec/common/cpuid.asm @@ -85,7 +85,7 @@ WelsCPUId: push rdx mov eax, ecx - mov ecx, r9d + mov rcx, [r9] cpuid mov [r9], ecx mov [r8], ebx @@ -103,7 +103,8 @@ WelsCPUId: push rcx push rdx - mov eax, edi + mov eax, edi + mov rcx, [rcx] cpuid mov [r8], edx pop rdx From 0e38e5c4e74723b7b8c4eabd0e93c09ddf7eaa35 Mon Sep 17 00:00:00 2001 From: volvet Date: Thu, 9 Jan 2014 16:59:07 +0800 Subject: [PATCH 3/5] add exception handler, the AMD cpu must be single core if it do not support HTT --- codec/common/cpu.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codec/common/cpu.cpp b/codec/common/cpu.cpp index 9efab5a9..b7cd9828 100644 --- a/codec/common/cpu.cpp +++ b/codec/common/cpu.cpp @@ -133,7 +133,11 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { if( pNumberOfLogicProcessors != NULL ){ if (!strcmp((const str_t*)chVenderName, CPU_Vender_AMD)){ - *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX + if( uiCPU & WELS_CPU_HTT){ + *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX + } else { + *pNumberOfLogicProcessors = 1; + } } else if( !strcmp((const str_t*)chVenderName, CPU_Vender_INTEL) ){ uiFeatureC = 0; WelsCPUId(0x4, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD); From f2a14600a5667ff20a20fc4a212d2b8e445b7099 Mon Sep 17 00:00:00 2001 From: volvet Date: Fri, 10 Jan 2014 15:32:35 +0800 Subject: [PATCH 4/5] check cpuid max level before use cpuid.4 --- codec/common/cpu.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/codec/common/cpu.cpp b/codec/common/cpu.cpp index b7cd9828..ffa69f96 100644 --- a/codec/common/cpu.cpp +++ b/codec/common/cpu.cpp @@ -55,6 +55,7 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { uint32_t uiFeatureA = 0, uiFeatureB = 0, uiFeatureC = 0, uiFeatureD = 0; int32_t CacheLineSize = 0; int8_t chVenderName[16] = { 0 }; + uint32_t uiMaxCpuidLevel = 0; if (!WelsCPUIdVerify()) { /* cpuid is not supported in cpu */ @@ -62,7 +63,8 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { } WelsCPUId (0, &uiFeatureA, (uint32_t*)&chVenderName[0], (uint32_t*)&chVenderName[8], (uint32_t*)&chVenderName[4]); - if (uiFeatureA == 0) { + uiMaxCpuidLevel = uiFeatureA; + if (uiMaxCpuidLevel == 0) { /* maximum input value for basic cpuid information */ return 0; } @@ -139,9 +141,17 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { *pNumberOfLogicProcessors = 1; } } else if( !strcmp((const str_t*)chVenderName, CPU_Vender_INTEL) ){ - uiFeatureC = 0; - WelsCPUId(0x4, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD); - *pNumberOfLogicProcessors = ((uiFeatureA&0xfc000000)>>26) + 1; + if( uiMaxCpuidLevel >= 4 ){ + uiFeatureC = 0; + WelsCPUId(0x4, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD); + *pNumberOfLogicProcessors = ((uiFeatureA&0xfc000000)>>26) + 1; + } else { + if( uiCPU & WELS_CPU_HTT){ + *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX + } else { + *pNumberOfLogicProcessors = 1; + } + } } else { //FIXME: other cpus *pNumberOfLogicProcessors = 1; From ccaef9fc6d95b3494a85db46c07f23756f487e0c Mon Sep 17 00:00:00 2001 From: volvet Date: Sat, 11 Jan 2014 18:56:22 +0800 Subject: [PATCH 5/5] protect code if cpuid.4 return eax as zero --- codec/common/cpu.cpp | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/codec/common/cpu.cpp b/codec/common/cpu.cpp index ffa69f96..0275169e 100644 --- a/codec/common/cpu.cpp +++ b/codec/common/cpu.cpp @@ -134,29 +134,21 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { } if( pNumberOfLogicProcessors != NULL ){ - if (!strcmp((const str_t*)chVenderName, CPU_Vender_AMD)){ - if( uiCPU & WELS_CPU_HTT){ - *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX - } else { - *pNumberOfLogicProcessors = 1; - } - } else if( !strcmp((const str_t*)chVenderName, CPU_Vender_INTEL) ){ + if( uiCPU & WELS_CPU_HTT){ + *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX + } else { + *pNumberOfLogicProcessors = 1; + } + if( !strcmp((const str_t*)chVenderName, CPU_Vender_INTEL) ){ if( uiMaxCpuidLevel >= 4 ){ uiFeatureC = 0; WelsCPUId(0x4, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD); - *pNumberOfLogicProcessors = ((uiFeatureA&0xfc000000)>>26) + 1; - } else { - if( uiCPU & WELS_CPU_HTT){ - *pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX - } else { - *pNumberOfLogicProcessors = 1; + if( uiFeatureA != 0 ){ + *pNumberOfLogicProcessors = ((uiFeatureA&0xfc000000)>>26) + 1; } } - } else { - //FIXME: other cpus - *pNumberOfLogicProcessors = 1; } - } + } WelsCPUId (0x80000000, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD);