{"id":18565120,"url":"https://github.com/hubblo-org/windows-rapl-driver","last_synced_at":"2025-04-10T04:32:28.584Z","repository":{"id":65507465,"uuid":"421079628","full_name":"hubblo-org/windows-rapl-driver","owner":"hubblo-org","description":"Windows driver to get RAPL metrics from a bare metal machine.","archived":false,"fork":false,"pushed_at":"2024-04-01T15:08:35.000Z","size":24911,"stargazers_count":12,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-05-01T11:27:32.095Z","etag":null,"topics":["climate","climate-change","driver","drivers","energy","energy-consumption","energy-data","energy-efficiency","energy-monitor","hubblo","intel","power","scaphandre","sustainability","sustainable-development","sustainable-software","windows","x86"],"latest_commit_sha":null,"homepage":"https://hubblo-org.github.io/scaphandre-documentation","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hubblo-org.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":"FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"hubblo-org"}},"created_at":"2021-10-25T15:26:40.000Z","updated_at":"2024-04-03T06:12:10.000Z","dependencies_parsed_at":"2023-10-13T15:36:47.676Z","dependency_job_id":"3bc37e87-3be3-4787-9c28-633c111f693d","html_url":"https://github.com/hubblo-org/windows-rapl-driver","commit_stats":{"total_commits":124,"total_committers":4,"mean_commits":31.0,"dds":"0.41129032258064513","last_synced_commit":"7ab6ee10d66992de5c587370edb3bae1bf5b011a"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hubblo-org%2Fwindows-rapl-driver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hubblo-org%2Fwindows-rapl-driver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hubblo-org%2Fwindows-rapl-driver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hubblo-org%2Fwindows-rapl-driver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hubblo-org","download_url":"https://codeload.github.com/hubblo-org/windows-rapl-driver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223424449,"owners_count":17142767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["climate","climate-change","driver","drivers","energy","energy-consumption","energy-data","energy-efficiency","energy-monitor","hubblo","intel","power","scaphandre","sustainability","sustainable-development","sustainable-software","windows","x86"],"created_at":"2024-11-06T22:17:49.358Z","updated_at":"2024-11-06T22:17:49.906Z","avatar_url":"https://github.com/hubblo-org.png","language":"C","funding_links":["https://github.com/sponsors/hubblo-org"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  windows-rapl-driver\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://repository-images.githubusercontent.com/421079628/f695abc0-c8e6-46a3-a6f4-6c7c0f617b87\" width=\"800\"\u003e\n\u003c/p\u003e\n\n# Windows driver for RAPL metrics gathering\n\n## Installation (for testing/troubleshooting purposes, in test mode)\n\n### Enable test mode\n\nFor now, the driver is unsigned, so you need to put windows in test mode (allowing custom drivers to run).\n\nTo do this, open a command prompt **as an administrator** and run :\n\n\tbcdedit.exe -set TESTSIGNING ON\n\tbcdedit.exe -set nointegritychecks on\n\nThen restart the computer or server.\n\nOnce restarted, if on a desktop, you should have some text written on the bottom right corner of the desktop view with **\"Test Mode\"** displayed.\n\nTo disable test mode just open another command prompt with adminstrator access and run :\n\n\tbcdedit.exe -set TESTSIGNING OFF\n\n### Installing the driver\n\nFirst download or [compile](#Compilation) :\n- DriverLoader.exe\n- ScaphandreDrv.sys\n\nYou need those files in the same folder.\n\nThen run, in an adminstrator command prompt :\n\n\tDriverLoader.exe install\n\nStart the service :\n\n\tDriverLoader.exe start\n\nAt any time you could check for the state of the service giving access to the driver on your system, with this command :\n\n\tdriverquery /v | grep -i scaph\n\nIf running properly it should show a line like :\n\n\tScaphandre Dr Scaphandre Driver Serv Scaphandre Driver Serv File System    System            Running    OK         TRUE             FALSE                  0                 4□096       0          14/01/2022 16:01:37    C:\\WINDOWS\\system32\\DRIVERS\\ScaphandreDrv.sys    4□096\n\n\n## Compilation\n\n### Windows 10\n\nInstall Visual Studio 2019\n\n### Windows 11\n\nInstall Visual Studio 2022\n\nAccording to [this documentation](https://learn.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk):\n\n- [Install the Windows 11 SDK](https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/)\n- [Install the Windows 11 WDK (Windows Driver Kit)](https://go.microsoft.com/fwlink/?linkid=2196230)\n\nOptionnal : [install the EWDK](https://learn.microsoft.com/en-us/legal/windows/hardware/enterprise-wdk-license-2022)\n\nRight clik on ScaphandreDrv in the right panel, in General Properties, look for the \"Platform Toolset\" field. It should say \"WindowsKernelModeDriver10.0\".\n\nIf the WDK doesn't appear in ScaphandreDrv Properties as a Platform Toolset choice, look for the vsix runner that should have a path like :\n\n```\n C:\\Program Files (x86)\\Windows Kits\\10\\Vsix\\VS2022\\10.0.22621.382\\WDK.vsix.\n```\n\nClose Visual Studio 2022, run the vsix runner.\n\nOn the top of the VS Window, select \"Release\" as a target and \"x64\" as a target platform.\n\nThen \"Build\" \u003e \"Build Solution\".\n\nTo build DriverLoader :\n\nRight Click on the Solution \u003e Add \u003e New Projet \u003e Empty Project\n\nOn the new Project \"DriverLoader\" inside the solution, right click \u003e add \u003e existing item, look for DriverLoader.cpp.\n\nRight click on DriverLoader in the right panel \u003e Properties \u003e Advanced, then ensure \"Character Set\" is set as \"Use Multi-Byte Character Set\".\n\nBuild \u003e Build Solution ( same target and platform as ScaphandreDrv ).\n\n### Compile from GNU/Linux (Ubuntu 22.04), run on windows (not recommended, as this is static compilation)\n\nCompile DriverLoader.exe in `userland/Service`:\n\n```\ncd userland/Service\n\nsudo apt install g++-mingw-w64 g++-mingw-w64-x86-64-win32 g++-mingw-w64-x86-64\n\nx86_64-w64-mingw32-g++ DriverLoader.cpp -I/usr/x86_64-w64-mingw32/include/ddk/ -I/usr/x86_64-w64-mingw32/include/ --sysroot=. -o DriverLoader.exe -Wall -pedantic\n```\n\n### Build an MSI package\n\n#### Setup\n\nIn Visual Studio 2022 or 2019, open Extensions \u003e Manage Extensions, then search for \"Microsoft Visual Studio Installer Projects 2019|2022\", then Download, close Visual Studtio.\n\nThen a VSIX installer should start. Restart Visual Studio.\n\nIf the ScaphandreDrvInstaller project is displayed as incompatible, right click, then \"Reload Project\". Then Build.\n\n### Test-sign the driver (reference documentation is [here](https://learn.microsoft.com/en-us/windows-hardware/drivers/install/how-to-test-sign-a-driver-package))\n\nIn C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.22621.0\\x64\n\n\t.\\MakeCert.exe -r -pe -ss PrivateCertStore -n CN=hubblo.org -eku 1.3.6.1.5.5.7.3.3 ScaphandreDrvTest.cer\n\nIn C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.22621.0\\x86, when you already have an Inf file\n\n\t.\\stampinf.exe -f C:\\Users\\MYUSER\\source\\repos\\windows-rapl-driver\\ScaphandreDrv\\ScaphandreDrv.inf -d 03/11/2023 -v 0.0.1\n\nThen (need to change inf file to succeed with x64, TODO document this part)\n\n\t.\\Inf2Cat.exe /driver:C:\\Users\\MYUSER\\source\\repos\\windows-rapl-driver\\ScaphandreDrv\\ /os:10_X64\n\nThen\n\n\t.\\signtool.exe sign /v /fd sha256 /s PrivateCertStore /n hubblo.org \"C:\\Users\\MYUSER\\source\\repos\\windows-rapl-driver\\ScaphandreDrv\\scaphandredrv.cat\"\n\nThen (as Administrator)\n\n\t.\\certmgr.exe /add \"C:\\Users\\MYUSER\\source\\repos\\windows-rapl-driver\\ScaphandreDrv\\ScaphandreDrvTest.cer\" /s /r localMachine root\n\nOnce the signing is proper, you should be able to install the driver with :\n\n\t\u0026 \"C:\\Program Files (x86)\\Windows Kits\\10\\Tools\\10.0.22621.0\\x64\\devcon.exe\" install .\\ScaphandreDrv.inf root\\SCAPHANDREDRV\n\nIf the signature is okay (even if untrusted), you should get this pop up window :\n\n![unknown_publisher](https://user-images.githubusercontent.com/906428/224494859-a9a7f2f1-2152-4487-99ff-6daa963195a8.png)\n\n## Context\n\nThis driver has been developped for a specific use case : enabling [Scaphandre](https://github.com/hubblo-org/scaphandre) on Windows.\n\nPlease have a look at [those slides](https://github.com/hubblo-org/scaphandre/files/8601923/Boavizta.-.Scaphandre_RAPL_Windows.pdf) for a better understanding of how and why this driver has been developped.\n\n## How to sign the driver (MS validated, avoiding test-mode)\n\n### Overall process\n\n1. Obtain an EV (extended validation) code signing certificate\n    1. ex: ssl.com # TODO document process, you need either a letter from a law firm to authenticate the structure, or follow the validation process of a third party, Google Business for example. The Google process did not work on my smartphone, so I went through a law firm.\n2. Create or use an Azure account with the Global Administrator account of the Azure AD domain / Entra ID (default if user creator of the Azure account)\n3. Register on the [Hardware Partner Program](https://partner.microsoft.com/en-US/dashboard/Registration/Hardware?step=AccountDetails)\n4. Check that the EV certificate is registered on the Partner Center (in “Manage Certificates”). Otherwise follow the procedure for downloading + signing + uploading a test file.\n5. Download a Virtual HLK (virtual machine or app for physical machine) Controller, be careful to choose exactly the version compatible with the target test machine (example: Windows 10 22H2): [https://learn.microsoft.com/fr-fr/windows-hardware/test/hlk/](https://learn.microsoft.com/fr-fr/windows-hardware/test/hlk/), install the controller and make it accessible on the network\n6. Associate a test machine to the controller by installing the HLK Client made available on the network by the controller, on the test machine\n7. In HLK Studio\n    1. Create a machine pool, put the discovered test machine there\n    2. b. Create a project\n    3. In Selection, select the machine pool, then in software device, find the .sys driver\n    4. In Tests check the tests available for this driver, load a test playlist if necessary\n    5. Launch Run Selected\n    6. In HLK Manager check that the test machine is Ready or Running and has jobs scheduled, check that it runs them (it restarts several times with login on a user of service)\n    7. In Results, check that everything is green, or go see the logs\n    8. In Package, add the driver folder (this must contain the .sys, the .inf and the .cat imperatively), sign if you can do it directly with the EV certificate. This is not possible with ssl.com which keeps the private key and requires going through yubikey or its cloud signature service. Choose to make the hlkx package without signing in this case.\n    9. Add symbols: right click on the folder line in the Package tab, then Add symbols and go to find the .pdb file from the Visual Studio build\n8. HLKX signature: this is theoretically possible with HLK Studio, but it does not work with SSL.com keys\n    1. Make sure you have included the key via ssl.com CKA in the local store, download CKA, then authenticate: https://www.ssl.com/download/ssl-com-esigner-cka\n    2. Download HLKSignTool from ssl.com: https://www.ssl.com/download/hlksigntool/\n    3. Once unzipped, use the binary in powershell: \u0026 \"C:\\Users\\MYUSER\\Downloads\\HLKSignTool_v1.0\\HLKSignTool.exe\" KeySerialNumber \"C:\\Users\\MYUSER\\VirtualBox VMs\\shared\\ScaphandreDriver_withsymbols.hlkx\"\n    4. Have your phone on hand for the OTP\n\n#### Sources\n\n- https://learn.microsoft.com/fr-fr/windows-hardware/drivers/dashboard/get-started-dashboard-submissions\n- https://learn.microsoft.com/fr-fr/windows-hardware/drivers/dashboard/get-started-dashboard-submissions#step-4-submit-for-certification-and-compatibility\n\n### Virtual HLK and HLK Studio\n\nFor Windows 11 and + compatible versions, the admin account is HLKAdminUser with the password set at startup. For previous versions, it is HLKAdminUser / Testpassword,1.\n\nWe download a vhdx, which can only be used in Hyper-V. If you don't have Hyper-V compatible Windows, you have to convert it to vhd or vdi for use in virtual box\n\n```\n.\\VBoxManage.exe clonemedium disk \"C:\\Users\\MYUSER\\Downloads\\2019DC-22621.1.ni_release.220506-1250-HLK.vhdx\" \"C:\\Users\\MYUSER\\Downloads\\2019DC-22621.1.ni_release.220506-1250-HLK.vhd\" --format vhd\n```\n\nChange the network interface in Virtual Box from NAT to Bridged Interface, then accept the discovery on the network from the windows guest.\n\n### Setup of a VHLK as controller and HLK Client on a test machine\n\nRemember to enable network discovery mode:\n`Control Panel \u003e Network and Internet \u003e Network and Sharing center \u003e Advanced Sharing Settings`\nThen click on Turn on network discovery, then Save changes\n\nIf it does not work (if when reopening this panel the button is still on “Turn off”, follow this procedure: https://learn.microsoft.com/en-us/troubleshoot/windows-client/networking/cannot-turn-on-network-discovery\n\nMore precisely, open Run and launch services.msc if one of the services mentioned in the doc is not started: right click, mode: manual, apply, start\n\n\u003e Make sure that the following dependency services are started:\n\u003e\n\u003e - DNS Client\n\u003e - Function Discovery Resource Publication\n\u003e - SSDP Discovery\n\u003e - UPnP Device Host\n\u003e\n\u003e Configure the Windows firewall to allow Network Discovery by following these steps:\n\u003e\n\u003e - Open Control Panel, select System and Security, and then select Windows Firewall.\n\u003e - In the left pane, select Allow an app or feature\n\u003e - Open Control Panel, select System and Security, and then select Windows Firewall.\n\u003e - In the left pane, select Allow an app or feature through Windows Firewall.\n\u003e - Select Change settings. If you're prompted for an administrator password or confirmation, enter the password or provide confirmation.\n\u003e - Select Network discovery, and then select OK.\n\u003e - Configure other firewalls in the network to allow Network Discovery.\n\u003e - Turn on Network Discovery in Network and Sharing Center.\n\nSources\n\n- https://learn.microsoft.com/fr-fr/windows-hardware/drivers/dashboard/hardware-submission-create\n\nTODO Automating the process :\n- https://learn.microsoft.com/fr-fr/windows-hardware/test/hlk/user/hck-testing-concepts#how-it-works\n- https://learn.microsoft.com/fr-fr/windows-hardware/test/hlk/developer/hlk-developer-guide\n- https://learn.microsoft.com/fr-fr/windows-hardware/test/hlk/user/hlk-automation-tool\n\n## Cross compilation MinGW (OUTDATED)\n\nDependencies:\n- x86_64-w64-mingw32-g++\n\nCompilation:\nx86_64-w64-mingw32-g++ DriverLoader.cpp -o DriverLoader.exe\n\n## Compilation CPUID (OUTDATED)\n\nCode getting CPU manufacturer's informations (through CPUID) uses inlined assembly.\n\nTo compile on Linux, we need to tell GCC to use the Intel syntax and not the At\u0026T one. We also need to specify we don't want an axecutable with independant position.\n\nExample : gcc -masm=intel -no-pie cpuid.c -o cpuid\n\nTo compile on Windows, we can inline 32 bit assembly but not 64 bit (MSVC compiler doesn't support it).\n\nTo fix this, the __cpuid__ function is written in pure assembly matching the Windows ABI (x86 and x86_64). To compile, use those commands :\n - x86 : nasm -f win32 cpuid_x86.nasm -o cpuid_x86.obj\n - x86_64 : nasm -f win64 cpuid_x86_64.nasm -o cpuid_x86_64.obj\n\nTo compile the agent, including the __cpuid__ function, do it according to the architecture :\n - x86_64 : x86_64-w64-mingw32-g++ RAPLAgent.cpp -s cpuid_x86_64.obj -o RAPLAgent.exe\n\n## Q \u0026 A\n\n## External documentation we used\n\n- https://woshub.com/how-to-sign-an-unsigned-driver-for-windows-7-x64/\n- https://github.com/MicrosoftDocs/windows-driver-docs/blob/staging/windows-driver-docs-pr/install/signature-categories-and-driver-installation.md\n- https://github.com/intel/powergov\n- https://community.intel.com/t5/Software-Tuning-Performance/RAPL-for-energy-measurement/td-p/919723\n- www.eecs.berkeley.edu/Pubs/TechRpts/2012/EECS-2012-168.pdf\n- https://github.com/kentcz/rapl-tools\n- https://01.org/blogs/2014/running-average-power-limit-%E2%80%93-rapl\n- https://github.com/energymon/energymon/blob/master/msr/energymon-msr.c\n- https://github.com/gz/rust-x86\n- https://docs.rs/x86/0.43.0/x86/\n- https://github.com/gz/rust-x86/tree/master/x86test\n- https://docs.rs/x86/0.43.0/x86/msr/constant.MSR_RAPL_POWER_UNIT.html\n- https://github.com/insula-rs/libwhp/tree/master/src\n- https://crates.io/crates/register/1.0.2\n- https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/rdmsr--read-msr-\n- https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3b-part-2-manual.html\n- https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html\n- https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-software-developers-manual-volume-1-basic-architecture.html\n- https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Register/Intel/ArchitecturalMsr.h\n- https://github.com/tianocore/edk2/tree/master/MdePkg/Include/Register/Intel/Msr\n- https://github.com/lizhuohua/linux-kernel-module-rust\n- https://docs.rs/x86test-types/0.0.3/x86test_types/macro.kassert.html\n- https://doc.rust-lang.org/unstable-book/library-features/llvm-asm.html\n- https://elixir.bootlin.com/linux/latest/source/drivers/powercap\n- http://icl.cs.utk.edu/papi/docs/d4/d46/linux-powercap_8c_source.html\n- https://www.osr.com/getting-started-writing-windows-drivers/\n- http://woshub.com/how-to-sign-an-unsigned-driver-for-windows-7-x64/\n- https://www.electronicdesign.com/technologies/windows/article/21802026/how-to-write-windows-drivers\n- https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile\n- https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile\n- https://docs.microsoft.com/en-us/windows/win32/sync/synchronization-and-overlapped-input-and-output\n- https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea\n- https://docs.microsoft.com/en-us/windows-hardware/drivers/gettingstarted/writing-a-very-small-kmdf--driver\n- https://docs.microsoft.com/en-us/windows-hardware/drivers/gettingstarted/writing-your-first-driver\n- https://www.intel.com/content/www/us/en/developer/articles/tool/power-gadget.html#inpage-nav-12\n- https://github.com/Microsoft/Windows-driver-samples\n- https://www.osronline.com/article.cfm%5Earticle=229.htm\n- https://fr.adasbcc.org/driver-blocked-from-loading-fix-3085\n- https://www.codeproject.com/articles/13090/building-and-deploying-a-basic-wdf-kernel-mode-dri\n- https://www.digicert.com/kb/code-signing/signcode-signtool-command-line.htm\n- https://www.techpout.com/update-drivers-using-command-prompt-in-windows-10/\n\nFichiers intéressants dans le noyau linux :\n- drivers/powercap/intel_rapl_msr.c\n- arch/x86:lib/msr_smp.c\n\n## Q \u0026 A\n\n### The code execution cannot proceed because MSVCP140D.dll was not found. Reinstalling the program may fix the problem.\n\nbuild in release mode then retry !\n\n### How to build ?\n\n\"F:\\Windows Kits\\10\\bin\\x86\\Inf2Cat.exe\" /driver:c:\\Users\\nulse\\Documents\\GitHub\\test\\ /os:10_19H1_X64,10_RS5_X64,ServerRS5_X64,10_RS4_X64\n\nsee https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/inf2cat\n\n### Impossible to reinstall a new version of the driver even if the previous one seems absent\n\ndriverquery doesn't show any trace of a previous version of the driver but you gert an error 1078 when trying to install a new one with DriverLoader.exe\nEnusre you don't see Scaphandre service in services.msc, ensure you have uninstalled the full package in Add or Remove Programs\nIf nothing does the trick, then remove the remaining Registry entry that has the name of the service, in HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services (see https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/ed214b30-b094-43c0-85e8-4702da85b5dd/add-service-failed-to-create-service?forum=wdk)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhubblo-org%2Fwindows-rapl-driver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhubblo-org%2Fwindows-rapl-driver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhubblo-org%2Fwindows-rapl-driver/lists"}