x86cpuid.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 269 行

JAVA
269
字号
/*
 * $Id: X86CpuID.java,v 1.3 2003/11/28 11:10:07 epr Exp $
 */
package org.jnode.vm.x86;

import org.jnode.util.NumberUtils;
import org.jnode.vm.CpuID;

/**
 * Class used to identify the current processor.
 * 
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class X86CpuID extends CpuID {

	public static final int FEAT_FPU = (1 << 0);
	public static final int FEAT_VME = (1 << 1);
	public static final int FEAT_DE = (1 << 2);
	public static final int FEAT_PSE = (1 << 3);
	public static final int FEAT_TSC = (1 << 4);
	public static final int FEAT_MSR = (1 << 5);
	public static final int FEAT_PAE = (1 << 6);
	public static final int FEAT_MCE = (1 << 7);
	public static final int FEAT_CX8 = (1 << 8);
	public static final int FEAT_APIC = (1 << 9);
	public static final int FEAT_SEP = (1 << 11);
	public static final int FEAT_MTRR = (1 << 12);
	public static final int FEAT_PGE = (1 << 13);
	public static final int FEAT_MCA = (1 << 14);
	public static final int FEAT_CMOV = (1 << 15);
	public static final int FEAT_PAT = (1 << 16);
	public static final int FEAT_PSE36 = (1 << 17);
	public static final int FEAT_PSN = (1 << 18);
	public static final int FEAT_CLFSH = (1 << 19);
	public static final int FEAT_DS = (1 << 21);
	public static final int FEAT_ACPI = (1 << 22);
	public static final int FEAT_MMX = (1 << 23);
	public static final int FEAT_FXSR = (1 << 24);
	public static final int FEAT_SSE = (1 << 25);
	public static final int FEAT_SSE2 = (1 << 26);
	public static final int FEAT_SS = (1 << 27);
	public static final int FEAT_HTT = (1 << 28);
	public static final int FEAT_TM = (1 << 29);
	public static final int FEAT_PBE = (1 << 31);
	// Extended features
	public static final long FEAT_EST = (1L << 39);
	public static final long FEAT_TM2 = (1L << 40);
	public static final long FEAT_CNXTID = (1L << 42);

	/** The cpu id data */
	private final int[] data;
	/** Name of the processor */
	private String name;
	private final int steppingID;
	private final int model;
	private final int family;
	private final int features;
	private final long exFeatures;

	/**
	 * Create a cpu id that contains the data of a processor identified by the given processor id.
	 * 
	 * @param procId
	 *            "i586", "pentium" for Pentium, "i686", "pentium2" for Pentium II, "pentium3" for
	 *            Pentium III "pentium4" for Pentium 4 can be null
	 * @return New cpu id.
	 */
	public static X86CpuID createID(String procId) {
		// Handle default
		if (procId == null) {
			procId = "pentium";
		}
		final int[] id;
		if (procId.equals("pentium4")) {
			// Pentium 4
			id = new int[12];
			id[0] = 0x02;
			id[7] = FEAT_FPU | FEAT_PSE | FEAT_CMOV;
		} else if (procId.equals("pentium3")) {
			// Pentium 3
			id = new int[16];
			id[0] = 0x03;
			id[7] = FEAT_FPU | FEAT_PSE | FEAT_CMOV;
		} else if (procId.equals("pentium2")) {
			// Pentium 2
			id = new int[12];
			id[0] = 0x02;
			id[7] = FEAT_FPU | FEAT_PSE | FEAT_CMOV;
			
		} else {
			// Pentium
			id = new int[8];
			id[0] = 0x01;
			id[7] = FEAT_FPU | FEAT_PSE;
		}
		// Set name GenuineIntel
		id[1] = 0x756e6547;
		id[2] = 0x6c65746e;
		id[3] = 0x49656e69;
		return new X86CpuID(id);
	}

	/**
	 * Initialize this instance
	 */
	X86CpuID(int[] data) {
		this.data = data;
		final int eax = data[4];
		this.steppingID = eax & 0xF;
		this.model = (eax >> 4) & 0xF;
		this.family = (eax >> 8) & 0xF;
		this.features = data[7];
		this.exFeatures = features | (((long) data[6]) << 32);
	}

	/**
	 * Gets the processor name.
	 * 
	 * @return The processor name
	 */
	public String getName() {
		if (name == null) {
			final StringBuffer buf = new StringBuffer();
			intToString(buf, data[1]);
			intToString(buf, data[3]);
			intToString(buf, data[2]);
			name = buf.toString();
		}
		return name;
	}

	private final void intToString(StringBuffer buf, int value) {
		buf.append((char) (value & 0xFF));
		buf.append((char) ((value >> 8) & 0xFF));
		buf.append((char) ((value >> 16) & 0xFF));
		buf.append((char) ((value >>> 24) & 0xFF));
	}

	/**
	 * @return Returns the family.
	 */
	public final int getFamily() {
		return this.family;
	}

	/**
	 * @return Returns the model.
	 */
	public final int getModel() {
		return this.model;
	}

	/**
	 * @return Returns the steppingID.
	 */
	public final int getSteppingID() {
		return this.steppingID;
	}

	/**
	 * @return Returns the features.
	 */
	public final int getFeatures() {
		return this.features;
	}

	/**
	 * Has this CPU a given feature.
	 * 
	 * @param feature
	 * @return boolean
	 */
	public final boolean hasFeature(long feature) {
		return ((this.exFeatures & feature) == feature);
	}

	/**
	 * Has this CPU a given feature.
	 * 
	 * @param feature
	 * @return boolean
	 */
	public final boolean hasFeature(int feature) {
		return ((this.features & feature) == feature);
	}

	/**
	 * Convert all features to a human readable string.
	 * 
	 * @return The available features.
	 */
	private final String getFeatureString() {
		final StringBuffer buf = new StringBuffer();
		getFeatureString(buf, FEAT_FPU, "FPU");
		getFeatureString(buf, FEAT_VME, "VME");
		getFeatureString(buf, FEAT_DE, "DE");
		getFeatureString(buf, FEAT_PSE, "PSE");
		getFeatureString(buf, FEAT_TSC, "TSC");
		getFeatureString(buf, FEAT_MSR, "MSR");
		getFeatureString(buf, FEAT_PAE, "PAE");
		getFeatureString(buf, FEAT_MCE, "MCE");
		getFeatureString(buf, FEAT_CX8, "CX8");
		getFeatureString(buf, FEAT_APIC, "APIC");
		getFeatureString(buf, FEAT_SEP, "SEP");
		getFeatureString(buf, FEAT_MTRR, "MTRR");
		getFeatureString(buf, FEAT_PGE, "PGE");
		getFeatureString(buf, FEAT_MCA, "MCA");
		getFeatureString(buf, FEAT_CMOV, "CMOV");
		getFeatureString(buf, FEAT_PAT, "PAT");
		getFeatureString(buf, FEAT_PSE36, "PSE36");
		getFeatureString(buf, FEAT_PSN, "PSN");
		getFeatureString(buf, FEAT_CLFSH, "CLFSH");
		getFeatureString(buf, FEAT_DS, "DS");
		getFeatureString(buf, FEAT_ACPI, "ACPI");
		getFeatureString(buf, FEAT_MMX, "MMX");
		getFeatureString(buf, FEAT_FXSR, "FXSR");
		getFeatureString(buf, FEAT_SSE, "SSE");
		getFeatureString(buf, FEAT_SSE2, "SSE2");
		getFeatureString(buf, FEAT_SS, "SS");
		getFeatureString(buf, FEAT_HTT, "HTT");
		getFeatureString(buf, FEAT_TM, "TM");
		getFeatureString(buf, FEAT_PBE, "PBE");
		// Extended features
		getFeatureString(buf, FEAT_EST, "EST");
		getFeatureString(buf, FEAT_TM2, "TM2");
		getFeatureString(buf, FEAT_CNXTID, "CNXTID");
		return buf.toString();
	}

	private final void getFeatureString(StringBuffer buf, int feature, String featName) {
		if (hasFeature(feature)) {
			if (buf.length() > 0) {
				buf.append(',');
			}
			buf.append(featName);
		}
	}

	private final void getFeatureString(StringBuffer buf, long feature, String featName) {
		if (hasFeature(feature)) {
			if (buf.length() > 0) {
				buf.append(',');
			}
			buf.append(featName);
		}
	}

	/**
	 * Convert to a string representation.
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "CPU:"
			+ " name:"
			+ getName()
			+ " family:"
			+ getFamily()
			+ " model:"
			+ getModel()
			+ " step:"
			+ getSteppingID()
			+ " features:"
			+ getFeatureString()
			+ " raw:"
			+ NumberUtils.hex(data, 8);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?