hw random的实现

来源:互联网 发布:滨州行知中学电话 编辑:程序博客网 时间:2024/06/15 20:25
我们一般都是通过dev/random 来得到随机数,但是这个随机数其实是伪随机的.因此就有厂商用硬件实现随机数的产生,kernel 一样已经提供了随机数的framework曾.路径在drivers/char/Hw_random/hisi-rng.c ,我们直接看probe函数吧static int hisi_rng_probe(struct platform_device *pdev){struct hisi_rng *rng;struct resource *res;int ret;//申请一个struct hisi_rng *rngrng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);if (!rng)return -ENOMEM;platform_set_drvdata(pdev, rng);//获得这个硬件的资源res = platform_get_resource(pdev, IORESOURCE_MEM, 0);rng->base = devm_ioremap_resource(&pdev->dev, res);if (IS_ERR(rng->base))return PTR_ERR(rng->base);//赋值rng->rng.name = pdev->name;rng->rng.init = hisi_rng_init;rng->rng.cleanup = hisi_rng_cleanup;rng->rng.read = hisi_rng_read;//最核心就是调用devm_hwrng_register 将当前硬件注册给kernelret = devm_hwrng_register(&pdev->dev, &rng->rng);if (ret) {dev_err(&pdev->dev, "failed to register hwrng\n");return ret;}return 0;}我们继续看devm_hwrng_registerint devm_hwrng_register(struct device *dev, struct hwrng *rng){struct hwrng **ptr;int error;ptr = devres_alloc(devm_hwrng_release, sizeof(*ptr), GFP_KERNEL);if (!ptr)return -ENOMEM;//注册hwrngerror = hwrng_register(rng);if (error) {devres_free(ptr);return error;}*ptr = rng;//给当前硬件添加资源devres_add(dev, ptr);return 0;}int hwrng_register(struct hwrng *rng){int err = -EINVAL;struct hwrng *old_rng, *tmp;//必须实现name ,data_read,read.if (!rng->name || (!rng->data_read && !rng->read))goto out;mutex_lock(&rng_mutex);/* Must not register two RNGs with the same name. */err = -EEXIST;//同名的hw不能注册两次list_for_each_entry(tmp, &rng_list, list) {if (strcmp(tmp->name, rng->name) == 0)goto out_unlock;}init_completion(&rng->cleanup_done);complete(&rng->cleanup_done);old_rng = current_rng;err = 0;if (!old_rng) {假定这里old_rng 为空,这样就会通过set_current_rng调用hw对应的init函数,并给current_rng赋值。err = set_current_rng(rng);if (err)goto out_unlock;}所以能产生random的硬件都挂在rng_list 这个list上list_add_tail(&rng->list, &rng_list);if (old_rng && !rng->init) {/* * Use a new device's input to add some randomness to * the system.  If this rng device isn't going to be * used right away, its init function hasn't been * called yet; so only use the randomness from devices * that don't need an init callback. */add_early_randomness(rng);}out_unlock:mutex_unlock(&rng_mutex);out:return err;}继续看static int set_current_rng(struct hwrng *rng){int err;BUG_ON(!mutex_is_locked(&rng_mutex));err = hwrng_init(rng);if (err)return err;drop_current_rng();//给current_rng 复制current_rng = rng;return 0;}static int hwrng_init(struct hwrng *rng){if (kref_get_unless_zero(&rng->ref))goto skip_init;if (rng->init) {int ret;//调用硬件的init函数而这里init函数就是rng->rng.init = hisi_rng_init;源码贴在后面基本就是先得到一个随机数后,然后写道硬件寄存器中.ret =  rng->init(rng);if (ret)return ret;}kref_init(&rng->ref);reinit_completion(&rng->cleanup_done);skip_init:add_early_randomness(rng);//这里会比较产生随机数的质量,也就是谁产生的随机数更随机。最大不能超过1024current_quality = rng->quality ? : default_quality;if (current_quality > 1024)current_quality = 1024;//因为我们的已经会自动产生随机数,因此下面这个条件成立if (current_quality == 0 && hwrng_fill)kthread_stop(hwrng_fill);if (current_quality > 0 && !hwrng_fill)start_khwrngd();return 0;}static int hisi_rng_init(struct hwrng *rng){struct hisi_rng *hrng = to_hisi_rng(rng);int val = RNG_EN;u32 seed;/* get a random number as initial seed */get_random_bytes(&seed, sizeof(seed));writel_relaxed(seed, hrng->base + RNG_SEED);/** * The seed is reload periodically, there are two choice * of seeds, default seed using the value from LFSR, or * will use seed generated by ring oscillator. */if (seed_sel == 1)val |= RNG_RING_EN | RNG_SEED_SEL;writel_relaxed(val, hrng->base + RNG_CTRL);return 0;}如果硬件不会自动更新随机数,则就要调用kernel定时生产if (current_quality > 0 && !hwrng_fill)start_khwrngd();这里会生产一个threadstatic void start_khwrngd(void){hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");if (IS_ERR(hwrng_fill)) {pr_err("hwrng_fill thread creation failed");hwrng_fill = NULL;}}static int hwrng_fillfn(void *unused){long rc;while (!kthread_should_stop()) {struct hwrng *rng;rng = get_current_rng();if (IS_ERR(rng) || !rng)break;mutex_lock(&reading_mutex);最终调用rng_get_data 来从硬件更新randomrc = rng_get_data(rng, rng_fillbuf,  rng_buffer_size(), 1);mutex_unlock(&reading_mutex);put_rng(rng);if (rc <= 0) {pr_warn("hwrng: no data available\n");msleep_interruptible(10000);continue;}/* Outside lock, sure, but y'know: randomness. */add_hwgenerator_randomness((void *)rng_fillbuf, rc,   rc * current_quality * 8 >> 10);}hwrng_fill = NULL;return 0;}最终通过rng_get_data 来从硬件读static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,int wait) {int present;BUG_ON(!mutex_is_locked(&reading_mutex));这里的read指向rng->rng.read = hisi_rng_read;if (rng->read)return rng->read(rng, (void *)buffer, size, wait);if (rng->data_present)present = rng->data_present(rng, wait);elsepresent = 1;if (present)return rng->data_read(rng, (u32 *)buffer);return 0;}最终hisi_rng_read 是直接通过读硬件寄存器static int hisi_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait){struct hisi_rng *hrng = to_hisi_rng(rng);u32 *data = buf;*data = readl_relaxed(hrng->base + RNG_RAN_NUM);return 4;}

原创粉丝点击