From ga9@buffy.york.ac.uk  Fri Apr 16 08:15:22 2004
Return-Path: <ga9@buffy.york.ac.uk>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 0BC2616A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 16 Apr 2004 08:15:22 -0700 (PDT)
Received: from mail-gw0.york.ac.uk (mail-gw0.york.ac.uk [144.32.128.245])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 5F29D43D5A
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 16 Apr 2004 08:15:21 -0700 (PDT)
	(envelope-from ga9@buffy.york.ac.uk)
Received: from kimiko.york.ac.uk (kimiko.york.ac.uk [144.32.226.167])
	by mail-gw0.york.ac.uk (8.12.10/8.12.10) with ESMTP id i3GFFAhx012507;
	Fri, 16 Apr 2004 16:15:10 +0100 (BST)
Received: from buffy.york.ac.uk (buffy.york.ac.uk [IPv6:2001:630:61:0:20c:f1ff:fe85:e351])
	by kimiko.york.ac.uk (8.12.11/8.12.11) with ESMTP id i3GFFAdi004542;
	Fri, 16 Apr 2004 16:15:10 +0100 (BST)
	(envelope-from ga9@buffy.york.ac.uk)
Received: from buffy.york.ac.uk (localhost.york.ac.uk [127.0.0.1])
	by buffy.york.ac.uk (8.12.11/8.12.11) with ESMTP id i3GFFAdW011326;
	Fri, 16 Apr 2004 16:15:10 +0100 (BST)
	(envelope-from ga9@buffy.york.ac.uk)
Received: (from ga9@localhost)
	by buffy.york.ac.uk (8.12.11/8.12.11/Submit) id i3GFFAfM011325;
	Fri, 16 Apr 2004 16:15:10 +0100 (BST)
	(envelope-from ga9)
Message-Id: <200404161515.i3GFFAfM011325@buffy.york.ac.uk>
Date: Fri, 16 Apr 2004 16:15:10 +0100 (BST)
From: Gavin Atkinson <gavin@ury.york.ac.uk>
Reply-To: Gavin Atkinson <gavin@ury.york.ac.uk>
To: FreeBSD-gnats-submit@freebsd.org
Cc: gavin@ury.york.ac.uk
Subject: Patch to store P3 serial number in sysctl
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         65627
>Category:       kern
>Synopsis:       [i386] [patch] store P3 serial number in sysctl
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Apr 16 08:20:21 PDT 2004
>Closed-Date:    Fri Oct 17 14:56:23 UTC 2008
>Last-Modified:  Fri Oct 17 14:56:23 UTC 2008
>Originator:     Gavin Atkinson
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
>Environment:
System: FreeBSD buffy.york.ac.uk 5.2-CURRENT FreeBSD 5.2-CURRENT #7: Tue Apr 13 19:45:13 BST 2004 root@buffy.york.ac.uk:/usr/obj/usr/src/sys/GENERIC i386

>Description:
	Pentium III processors have a serial number stored within them.
If not disabled, this can be used to license software against, etc.
This patch provides userland access to the serial number. It also includes
a printf to show the serial number when bootverbose is set, but it may be
preferable to not include that.

>How-To-Repeat:
	n/a
>Fix:

	(Patch also at http://www.devrandom.co.uk/freebsd/psn.diff)

--- sys/i386/i386/identcpu.c.old	Fri Apr  9 18:00:03 2004
+++ sys/i386/i386/identcpu.c	Tue Apr 13 22:17:50 2004
@@ -94,6 +94,13 @@
 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, 
     cpu_model, 0, "Machine model");
 
+SYSCTL_NODE(_hw, OID_AUTO, cpu0, CTLFLAG_RD,
+    0, "CPU 0 Information");
+
+static char cpu_serial[32];
+SYSCTL_STRING(_hw_cpu0, OID_AUTO, serial, CTLFLAG_RD, 
+    cpu_serial, 0, "CPU serial number");
+
 static int hw_clockrate;
 SYSCTL_INT(_hw, OID_AUTO, clockrate, CTLFLAG_RD, 
     &hw_clockrate, 0, "CPU instruction clock rate");
@@ -695,6 +702,21 @@
 			    (cpu_procinfo & CPUID_HTT_CORES) >> 16 > 1)
 				printf("\n  Hyperthreading: %d logical CPUs",
 				    (cpu_procinfo & CPUID_HTT_CORES) >> 16);
+			/*
+			 * Obtain the serial number if the CPU supports it
+			 */
+			if (cpu_feature & CPUID_PSN) {
+				do_cpuid(3, regs);
+				snprintf(cpu_serial,
+				    sizeof(cpu_serial),
+				    "%04x-%04x-%04x-%04x-%04x-%04x",
+				    (cpu_id >> 16), (cpu_id & 0xffff),
+				    (regs[3] >> 16), (regs[3] & 0xffff),
+				    (regs[2] >> 16), (regs[2] & 0xffff));
+				if (bootverbose)
+					printf("\n  Processor Serial Number: %s",
+					    cpu_serial);
+			}
 		}
 		if (strcmp(cpu_vendor, "AuthenticAMD") == 0 &&
 		    cpu_exthigh >= 0x80000001)


>Release-Note:
>Audit-Trail:

From: "Internet Partners, Inc. Tech Support" <support@ipinc.net>
To: <bug-followup@FreeBSD.org>, <gavin@ury.york.ac.uk>
Cc:  
Subject: Re: kern/65627: [i386] [patch] store P3 serial number in sysctl
Date: Sat, 27 Jan 2007 02:32:15 -0800

 Here is this patch updated for the FreeBSD 6.2-RELEASE kernel:
 
 
 diff -u sys/i386/i386/identcpu.c.orig sys/i386/i386/identcpu.c
 
 --- sys/i386/i386/identcpu.c.orig       Tue Aug  8 01:41:34 2006
 +++ sys/i386/i386/identcpu.c    Sat Jan 27 01:33:18 2007
 @@ -89,6 +89,13 @@
  SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD,
      cpu_model, 0, "Machine model");
 
 +SYSCTL_NODE(_hw, OID_AUTO, cpu0, CTLFLAG_RD,
 +    0, "CPU 0 Information");
 +
 +static char cpu_serial[32];
 +SYSCTL_STRING(_hw_cpu0, OID_AUTO, serial, CTLFLAG_RD,
 +    cpu_serial, 0, "CPU serial number");
 +
  static int hw_clockrate;
  SYSCTL_INT(_hw, OID_AUTO, clockrate, CTLFLAG_RD,
      &hw_clockrate, 0, "CPU instruction clock rate");
 @@ -856,6 +863,21 @@
                          */
                         if (cpu_feature & CPUID_HTT)
                                 htt = (cpu_procinfo & CPUID_HTT_CORES) >>
 16;
 +                       /*
 +                        * Obtain the serial number if the CPU supports it
 +                        */
 +                       if (cpu_feature & CPUID_PSN) {
 +                               do_cpuid(3, regs);
 +                               snprintf(cpu_serial,
 +                                   sizeof(cpu_serial),
 +                                   "%04x-%04x-%04x-%04x-%04x-%04x",
 +                                   (cpu_id >> 16), (cpu_id & 0xffff),
 +                                   (regs[3] >> 16), (regs[3] & 0xffff),
 +                                   (regs[2] >> 16), (regs[2] & 0xffff));
 +                               if (bootverbose)
 +                                       printf("\n  Processor Serial Number:
 %s",
 +                                           cpu_serial);
 +                       }
                         if (strcmp(cpu_vendor, "AuthenticAMD") == 0 &&
                             (amd_feature2 & AMDID2_CMP))
                                 cmp = (cpu_procinfo2 & AMDID_CMP_CORES) + 1;
 
 
 To apply, place patch in /usr/src and run patch < psn.diff
 
 sysctl is hw.cpu0.serial
 
 here is a snippet from booting verbosely with this patch:
 
 CPU: Intel Pentium III (927.11-MHz 686-class CPU)
   Origin = "GenuineIntel"  Id = 0x683  Stepping = 3
 
 Features=0x387fbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,
 CMOV,PAT,PSE36,PN,MMX,FXSR,SSE>
   Processor Serial Number: 0000-0623-0001-de43-1r2c-6123
 real memory  = 268435456 (256 MB)
 Physical memory chunk(s):
 
 so you can see what it looks like.
 
 Ted
 
 

From: John Baldwin <jhb@FreeBSD.org>
To: bug-followup@FreeBSD.org, gavin@ury.york.ac.uk
Cc:  
Subject: Re: kern/65627: [i386] [patch] store P3 serial number in sysctl
Date: Mon, 28 Jul 2008 16:29:21 -0400

 Strictly speaking, cpuid is not a privileged instruction, so userland can 
 already query the serial number by using cpuid directly if it is really 
 needed.  Given that CPU serial numbers were short-lived and are not included 
 in newer processsors (P4+), I'm not sure it is really worth it to have the 
 kernel print out the serial number on boot.
 
 -- 
 John Baldwin
State-Changed-From-To: open->closed 
State-Changed-By: gavin 
State-Changed-When: Fri Oct 17 14:52:34 UTC 2008 
State-Changed-Why:  
Close my own PR.  I'm no longer using this as processors after the PIII 
no longer provide the serial number, and also as jhb@ points out, this 
can be done from userland.  This probably belongs in a generic userland 
command, possibly similar to NetBSD's cpuset(1) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=65627 
>Unformatted:
