Skip to contentSkip to conversation

The VIA Hardware RNG driver is for the CPU, not Chipset

PATCH In review Harald Welte 3 participants 9 messages May 10, 2009 – May 15, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 The VIA Hardware RNG driver is for the CPU, not Chipset

This is a cosmetic change, fixing the MODULE_DESCRIPTION() of via-rng.c

---
 drivers/char/hw_random/via-rng.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/char/hw_random/via-rng.c +1 -1
index eea0814..824f003 100644
@@ -213,5 +213,5 @@ static void __exit mod_exit(void)
 module_init(mod_init);
 module_exit(mod_exit);
 
-MODULE_DESCRIPTION("H/W RNG driver for VIA chipsets");
+MODULE_DESCRIPTION("H/W RNG driver for VIA CPU with PadLock");
 MODULE_LICENSE("GPL");
Signature · 6 lines
1.5.6.5

-- 
- Harald Welte <[email protected]>	    http://linux.via.com.tw/
============================================================================
VIA Open Source Liaison
#1 lore raw
Quoted 1 line · Harald Welte

This is a cosmetic change, fixing the MODULE_DESCRIPTION() of via-rng.c

Coincidentally, I was trying to make my RNG work for x86_64 today and I was wondering about this. How can multiple RNGs in current dual-processor setups and in the future multicore Nano be handled?

The MSR wizardry in via_rng_init doesn't seem to work on my Nano. I'm simply skipping it with the patch below, because my RNG is enabled by default. I don't know the proper way to initialize it because of lacking documentation. Would you happen to know a better way of doing this?

drivers/char/hw_random/Kconfig +1 -1
diff -u linux-2.6.30-rc5.orig/drivers/char/hw_random/Kconfig linux-2.6.30-rc5/drivers/char/hw_random/Kconfig
@@ -88,7 +88,7 @@
 
 config HW_RANDOM_VIA
 	tristate "VIA HW Random Number Generator support"
-	depends on HW_RANDOM && X86_32
+	depends on HW_RANDOM && X86
 	default HW_RANDOM
 	---help---
 	  This driver provides kernel-side support for the Random Number
drivers/char/hw_random/via-rng.c +11 -0
diff -u linux-2.6.30-rc5.orig/drivers/char/hw_random/via-rng.c linux-2.6.30-rc5/drivers/char/hw_random/via-rng.c
@@ -132,6 +132,17 @@
 	struct cpuinfo_x86 *c = &cpu_data(0);
 	u32 lo, hi, old_lo;
 
+	/* Nano */
+	if ((c->x86 == 6) && (c->x86_model >= 0xf)) {
+		if (!cpu_has_xstore_enabled) {
+			printk(KERN_ERR PFX
+			       "don't know how to enable VIA Nano RNG,"
+			       " aborting\n");
+			return -ENODEV;
+		}
+		return 0;
+	}
+
 	/* Control the RNG via MSR.  Tread lightly and pay very close
 	 * close attention to values written, as the reserved fields
 	 * are documented to be "undefined and unpredictable"; but it
#2 lore raw

Dear Arjan,

Quoted 5 lines · Arjan Koers
Quoted 1 line · Harald Welte

This is a cosmetic change, fixing the MODULE_DESCRIPTION() of via-rng.c

Coincidentally, I was trying to make my RNG work for x86_64 today and I was wondering about this.

I like those coincidences :)

Quoted 2 lines

How can multiple RNGs in current dual-processor setups and in the future multicore Nano be handled?

That's actually a good question. I'll probably forward that to the CPU division and see what they come up with. I would assume you just run the initialization on every CPU, and that's it.

Now the more interesting question is: how can the hw_random framework deal with it? As far as I remember, it can only select one out of N available concurrent providers of hardware randomness. correct?

Quoted 1 line

The MSR wizardry in via_rng_init doesn't seem to work on my Nano.

This is due to the fact that the MSR itself does no longer exist on the Nano. It only exists on C3 to C7 silicon.

Quoted 2 lines

I'm simply skipping it with the patch below, because my RNG is enabled by default.

yes, this is the correct method. On Nano you only have to see if it is enabled in CPUID and forget about

Quoted 2 lines

I don't know the proper way to initialize it because of lacking documentation.

the quick reference for padlock on the nano is public, despite not at a very well-known location:
ftp://ftp.vtbridge.org/Docs/CPU/Nano/padlock_quick_reference_V095.pdf

Signature · 3 lines
- Harald Welte <[email protected]>	    http://linux.via.com.tw/
============================================================================
VIA Open Source Liaison
#3 lore raw

Hello Harald,

Quoted 1 line · Harald Welte

Dear Arjan,

[...]

Quoted 6 lines
Quoted 2 lines

How can multiple RNGs in current dual-processor setups and in the future multicore Nano be handled?

That's actually a good question. I'll probably forward that to the CPU division and see what they come up with. I would assume you just run the initialization on every CPU, and that's it.

It would probably have to be setup the same on all CPUs (enabled on all / disabled on all), because the code might execute on any CPU.

Quoted 3 lines

Now the more interesting question is: how can the hw_random framework deal with it? As far as I remember, it can only select one out of N available concurrent providers of hardware randomness. correct?

I don't know, but it would be nice if it could use PadLock functionality on multiple CPUs at the same time for increased speed.

Quoted 4 lines
Quoted 1 line

The MSR wizardry in via_rng_init doesn't seem to work on my Nano.

This is due to the fact that the MSR itself does no longer exist on the Nano. It only exists on C3 to C7 silicon.

Thanks for the information. Do you happen to know what changed for the other MSRs that are used for ACE, PowerSaver, ...?

Quoted 5 lines
Quoted 2 lines

I'm simply skipping it with the patch below, because my RNG is enabled by default.

yes, this is the correct method. On Nano you only have to see if it is enabled in CPUID and forget about

Since the Nano RNG can't be configured, the patch could be changed to something like this:

drivers/char/hw_random/via-rng.c renamed from drivers/char/hw_random/via-rng.c.orig +17 -1
@@ -132,6 +132,13 @@
 	struct cpuinfo_x86 *c = &cpu_data(0);
 	u32 lo, hi, old_lo;
 
+	/* VIA Nano CPUs don't have the MSR_VIA_RNG anymore.  The RNG
+	 * is always enabled if CPUID rng_en is set.  There is no
+	 * RNG configuration like it used to be the case in this
+	 * register */
+	if ((c->x86 == 6) && (c->x86_model >= 0x0f))
+		return 0;
+
 	/* Control the RNG via MSR.  Tread lightly and pay very close
 	 * close attention to values written, as the reserved fields
 	 * are documented to be "undefined and unpredictable"; but it
@@ -184,8 +191,17 @@
 {
 	int err;
 
-	if (!cpu_has_xstore)
+	if (!cpu_has_xstore) {
+		printk(KERN_NOTICE PFX "VIA RNG not detected.\n");
+		return -ENODEV;
+	}
+
+	if (!cpu_has_xstore_enabled) {
+		printk(KERN_NOTICE PFX "VIA RNG detected, but not enabled."
+		       " Hmm, strange...\n");
 		return -ENODEV;
+	}
+
 	printk(KERN_INFO "VIA RNG detected\n");
 	err = hwrng_register(&via_rng);
 	if (err) {

The check for cpu_has_xstore_enabled was moved to mod_init, because it turns out that arch/x86/kernel/cpu/centaur.c already tries to enable it in init_c3. This might break things if people have found ways to disable it again after it got enabled in init_c3. I've also changed mod_init to look more like drivers/crypto/padlock-aes.c and drivers/crypto/padlock-sha.c

If it's possible for a Nano to have the cpuid rng flag set and the rng_en flag unset, init_c3 in arch/x86/kernel/cpu/centaur.c may have to be changed too. This seems very unlikely to me, because there would be no way to enable the RNG then.

Quoted 6 lines
Quoted 2 lines

I don't know the proper way to initialize it because of lacking documentation.

the quick reference for padlock on the nano is public, despite not at a very well-known location:
ftp://ftp.vtbridge.org/Docs/CPU/Nano/padlock_quick_reference_V095.pdf

Thanks for the link. Together with version 3.1 of the PadLock SDK from the viaarena website, that's enough information about PadLock. Unfortunately I've not been able to find documentation about other features like the MSRs and Powersaver.

Kind regards,

Arjan Koers

#4 lore raw

Hi Arjan,

Quoted 10 lines · Arjan Koers
Quoted 6 lines · Harald Welte
Quoted 2 lines

How can multiple RNGs in current dual-processor setups and in the future multicore Nano be handled?

That's actually a good question. I'll probably forward that to the CPU division and see what they come up with. I would assume you just run the initialization on every CPU, and that's it.

It would probably have to be setup the same on all CPUs (enabled on all / disabled on all), because the code might execute on any CPU.

yes, but I don't really consider this a practical problem. I think there is no practical case where half of the CPUs want a hardware RNG, and half of them don't. If somebody really has that kind of special need, he can probably patch his kernel himself.

Quoted 6 lines
Quoted 3 lines

Now the more interesting question is: how can the hw_random framework deal with it? As far as I remember, it can only select one out of N available concurrent providers of hardware randomness. correct?

I don't know, but it would be nice if it could use PadLock functionality on multiple CPUs at the same time for increased speed.

I'm not so sure if that really makes sense just to increase throughput of a single workload / work item. However, for multiple concurrent threads, of course each thread on each CPU should be able to use the padlock instructions for AES / PHE / RNG. And if you enable the feature on all CPU's during the respective module_init(), it should simply work afterwards.

Quoted 5 lines
Quoted 2 lines

This is due to the fact that the MSR itself does no longer exist on the Nano. It only exists on C3 to C7 silicon.

Thanks for the information. Do you happen to know what changed for the other MSRs that are used for ACE, PowerSaver, ...?

I unfortunately don't [yet] but I'll inquire.

Quoted 2 lines

Since the Nano RNG can't be configured, the patch could be changed to something like this:

yes, thanks. looks fine to me.

Quoted 4 lines

If it's possible for a Nano to have the cpuid rng flag set and the rng_en flag unset, init_c3 in arch/x86/kernel/cpu/centaur.c may have to be changed too. This seems very unlikely to me, because there would be no way to enable the RNG then.

I'll re-confirm this with the CPU R&D, too.

Regards,

Signature · 3 lines
- Harald Welte <[email protected]>	    http://linux.via.com.tw/
============================================================================
VIA Open Source Liaison
#5 lore raw
In reply to Arjan Koers

Dear Arjan,

it seems like it is probably a pragmatic solution to first merge your suggested changes (Kconfig enable for X86_64 as well as skipping any MSR activity), before adressing the auto-loading in a potential follow-up patch.

I have sent your modifications (split in two patches, with some additional comment/explanation) to this list.

The question is: Since according to MAINTAINERS drivers/char/hw_random is orphaned, which route should patches go?

Herbert: Do you accept hw_random related patches, or should they go some different route?

Regards,

Signature · 3 lines
- Harald Welte <[email protected]>	    http://linux.via.com.tw/
============================================================================
VIA Open Source Liaison
#6 lore raw
Quoted 2 lines · Harald Welte

I have sent your modifications (split in two patches, with some additional comment/explanation) to this list.

Thanks. I've applied your patches and it works for me.

maintainer
#7 lore raw
In reply to Harald Welte
Quoted 3 lines · Harald Welte

Herbert: Do you accept hw_random related patches, or should they go some different route?

Yeah I'm taking hw_random patches.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt