Skip to content

Commit beb77fa

Browse files
committed
More spu information, especially for sgx features
Cpu class example Exec class for running process and return stdout
1 parent ef2bc60 commit beb77fa

File tree

14 files changed

+505
-80
lines changed

14 files changed

+505
-80
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.user

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ project(modern.cpp.core)
3434
include(cmake/env.cmake)
3535

3636
add_subdirectory(source)
37+
add_subdirectory(examples)

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ make -j`nproc`
1010
```
1111

1212
## Classes
13-
- **CPUID** - Get CPUID.
14-
- **Double** - Less, Greater, Equal, Round.
13+
- **CPU** - Get CPU informations.
14+
- **Double** - Less, Greater, Equal, Between, Round.
15+
- **Exec** - Run command and return stdout.
1516
- **Serial** - Serial communication class.
1617
- **Timing** - Measering time, cpu and real time.
1718

1819
## Template classes
19-
- **CSVWriter** - write out comma seperated values.
20-
- **Timer** - timeout thread one time or interval.
20+
- **CSVWriter** - Write out comma seperated values.
21+
- **Timer** - Timeout thread on time or interval.
2122

2223
## Unixservice class
2324
- Main function to run as a unix daemon.

examples/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#/*
2+
# * Copyright (c) 2020 Florian Becker <[email protected]> (VX APPS).
3+
# * All rights reserved.
4+
# *
5+
# * Redistribution and use in source and binary forms, with or without
6+
# * modification, are permitted provided that the following conditions are met:
7+
# *
8+
# * 1. Redistributions of source code must retain the above copyright notice, this
9+
# * list of conditions and the following disclaimer.
10+
# *
11+
# * 2. Redistributions in binary form must reproduce the above copyright notice,
12+
# * this list of conditions and the following disclaimer in the documentation
13+
# * and/or other materials provided with the distribution.
14+
# *
15+
# * 3. Neither the name of the copyright holder nor the names of its
16+
# * contributors may be used to endorse or promote products derived from
17+
# * this software without specific prior written permission.
18+
# *
19+
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
# * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
# * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
# * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
# * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
# */
30+
31+
project(examples)
32+
33+
add_subdirectory(cpuinfo)

examples/cpuinfo/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#/*
2+
# * Copyright (c) 2020 Florian Becker <[email protected]> (VX APPS).
3+
# * All rights reserved.
4+
# *
5+
# * Redistribution and use in source and binary forms, with or without
6+
# * modification, are permitted provided that the following conditions are met:
7+
# *
8+
# * 1. Redistributions of source code must retain the above copyright notice, this
9+
# * list of conditions and the following disclaimer.
10+
# *
11+
# * 2. Redistributions in binary form must reproduce the above copyright notice,
12+
# * this list of conditions and the following disclaimer in the documentation
13+
# * and/or other materials provided with the distribution.
14+
# *
15+
# * 3. Neither the name of the copyright holder nor the names of its
16+
# * contributors may be used to endorse or promote products derived from
17+
# * this software without specific prior written permission.
18+
# *
19+
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
# * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
# * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
# * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
# * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
# */
30+
31+
project(cpuinfo)
32+
33+
include_directories(../../source)
34+
35+
add_executable(cpuinfo
36+
main.cpp
37+
)
38+
39+
target_link_libraries(cpuinfo PRIVATE modern.cpp.core)

examples/cpuinfo/main.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2020 Florian Becker <[email protected]> (VX APPS).
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/* stl header */
32+
#include <iostream>
33+
34+
/* modern.cpp.core header */
35+
#include <CPU.h>
36+
37+
/** SGX information leaf. */
38+
constexpr unsigned int sgxLeaf = 12;
39+
40+
/** Maximum subleafs to read out. */
41+
constexpr unsigned int maximumSubLeafs = 10;
42+
43+
int main() {
44+
45+
VX::CPU cpu;
46+
std::cout << std::hex << cpu.eax() << " " << cpu.ebx() << " " << cpu.ecx() << " " << cpu.edx() << " " << std::dec << std::endl;
47+
48+
std::cout << "Stepping: " << cpu.stepping() << std::endl;
49+
std::cout << "Model: " << cpu.model() << std::endl;
50+
std::cout << "Family: " << cpu.family() << std::endl;
51+
std::cout << "Type: " << cpu.type() << std::endl;
52+
std::cout << "Extended model: " << cpu.extendedModel() << std::endl;
53+
std::cout << "Extended family: " << cpu.extendedFamily() << std::endl;
54+
55+
std::cout << "SMX support: " << cpu.smxSupport() << std::endl;
56+
57+
std::cout << std::endl << "Extended features" << std::endl;
58+
59+
std::cout << "SGX support: " << cpu.sgxSupport() << std::endl;
60+
std::cout << "SGX launch control support: " << cpu.sgxLaunchControlSupport() << std::endl;
61+
62+
std::cout << std::endl << "SGX features" << std::endl;
63+
64+
std::cout << "SGX version 1 support: " << cpu.sgxVersion1Support() << std::endl;
65+
std::cout << "SGX version 2 support: " << cpu.sgxVersion2Support() << std::endl;
66+
std::cout << "Maximum enclave size: " << cpu.maximumEnclaveSize() << std::endl;
67+
std::cout << "Maximum enclave size 64: " << cpu.maximumEnclaveSize64() << std::endl;
68+
69+
for ( unsigned int i = 1; i < maximumSubLeafs; i++ ) {
70+
71+
std::cout << std::endl << "SGX subleaf " << i << std::endl;
72+
cpu.updateNativeId( sgxLeaf, i );
73+
std::cout << std::hex << cpu.eax() << " " << cpu.ebx() << " " << cpu.ecx() << " " << cpu.edx() << " " << std::endl;
74+
}
75+
return 0;
76+
}

source/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ project(modern.cpp.core)
3232

3333
add_library(modern.cpp.core
3434
../README.md
35-
CPUID.h
35+
CPU.cpp
36+
CPU.h
3637
Double.cpp
3738
Double.h
39+
Exec.cpp
40+
Exec.h
3841
ModernCppCore.h
3942
Serial.cpp
4043
Serial.h

source/CPUID.h renamed to source/CPU.cpp

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,75 +28,41 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
#pragma once
32-
3331
#ifdef _WIN32
34-
#include <climits>
3532
#include <intrin.h>
36-
using uint32_t = unsigned __int32;
37-
#else
38-
#include <stdint.h>
33+
using unsigned int = unsigned __int32;
3934
#endif
4035

41-
/**
42-
* @brief VX (VX Apps) Namespace.
43-
*/
36+
/* local header */
37+
#include "CPU.h"
38+
4439
namespace VX {
4540

46-
/**
47-
* @brief The CPUID class for receiving CPU id.
48-
* @author Florian Becker <fb\@vxapps.com> (VX Apps)
49-
*/
50-
class CPUID {
41+
/** Leaf of extended information. */
42+
constexpr unsigned int extendedLeaf = 7;
5143

52-
public:
53-
/**
54-
* @brief Default constructor for CPUID.
55-
* @param _i Core id.
56-
*/
57-
explicit CPUID( int _i ) {
44+
/** Leaf of sgx information. */
45+
constexpr unsigned int sgxLeaf = 12;
5846

59-
#ifdef _WIN32
60-
__cpuid( ( int * )regs, _i );
47+
CPU::CPU( unsigned int _leaf, unsigned int _subleaf ) {
48+
49+
updateNativeId( sgxLeaf, 0 );
50+
m_sgxLeaf = m_currentLeaf;
51+
updateNativeId( extendedLeaf, 0 );
52+
m_extendedLeaf = m_currentLeaf;
53+
updateNativeId( 1, 0 );
54+
m_leaf = m_currentLeaf;
55+
updateNativeId( _leaf, _subleaf );
56+
}
57+
58+
void CPU::updateNativeId( unsigned int _leaf, unsigned int _subleaf ) {
6159

60+
#ifdef _WIN32
61+
__cpuid( m_currentLeaf.data(), _leaf, _subleaf );
6262
#else
6363
asm volatile
64-
( "cpuid" : "=a"( regs[0] ), "=b"( regs[1] ), "=c"( regs[2] ), "=d"( regs[3] )
65-
: "a"( _i ), "c"( 0 ) );
66-
/* ECX is set to zero for CPUID function 4 */
64+
( "cpuid" : "=a"( m_currentLeaf[0] ), "=b"( m_currentLeaf[1] ), "=c"( m_currentLeaf[2] ), "=d"( m_currentLeaf[3] )
65+
: "a"( _leaf ), "c"( _subleaf ) );
6766
#endif
68-
}
69-
70-
/**
71-
* @brief Return EAX id.
72-
* @return The EAX id.
73-
*/
74-
uint32_t EAX() const { return regs[0]; }
75-
76-
/**
77-
* @brief Return EBX id.
78-
* @return The EBX id.
79-
*/
80-
uint32_t EBX() const { return regs[1]; }
81-
82-
/**
83-
* @brief Return ECX id.
84-
* @return The ECX id.
85-
*/
86-
uint32_t ECX() const { return regs[2]; }
87-
88-
/**
89-
* @brief Return EDX id.
90-
* @return The EDX id.
91-
*/
92-
uint32_t EDX() const { return regs[3]; }
93-
94-
private:
95-
// std::vector<uint32_t> m_regs;
96-
/**
97-
* @brief Reg parts.
98-
* @todo: Convert to vector<int>.
99-
*/
100-
uint32_t regs[4] = {};
101-
};
67+
}
10268
}

0 commit comments

Comments
 (0)