cpuid-key.c 1009 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * outputs a cpuid key for use in context keyed payload encoding.
  3. *
  4. * Author: Dimitris Glynos <dimitris at census-labs.com>
  5. */
  6. #include <stdio.h>
  7. int main()
  8. {
  9. unsigned long eax;
  10. asm (
  11. "xorl %%esi, %%esi;" /* esi is key store, zero it out */
  12. "xorl %%edi, %%edi;" /* edi is loop iterator, ditto */
  13. "cpuid_loop: movl %%edi, %%eax;" /* iterator is first arg
  14. to cpuid */
  15. "xorl %%ecx, %%ecx;" /* ecx is also used as arg to cpuid but
  16. we'll use it always as zero */
  17. "cpuid;"
  18. "xorl %%eax, %%esi;"
  19. "cmpl %%esi, %%eax;" /* first time round esi = eax */
  20. /* not very safe heh? */
  21. "jne not_first_time;"
  22. "leal 0x1(%%eax, 1), %%edi;" /* first time round ... */
  23. "not_first_time: xorl %%ebx, %%esi;"
  24. "xorl %%ecx, %%esi;"
  25. "xorl %%edx, %%esi;"
  26. "subl $1, %%edi;"
  27. "jne cpuid_loop;"
  28. "movl %%esi, %%eax;"
  29. : "=a" (eax)
  30. );
  31. printf("%#.8lx\n", eax);
  32. return 0;
  33. }