8916系列------system reboot kernel篇

首先到sys.c中看

void kernel_restart(char *cmd)
{
kernel_restart_prepare(cmd);
printk( "matt-Restarting system with command '%s'.\n", cmd);
migrate_to_reboot_cpu();
syscore_shutdown();
if (!cmd)
printk(KERN_EMERG "Restarting system.\n");
else
printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
kmsg_dump(KMSG_DUMP_RESTART);
machine_restart(cmd);
}

接着一个一个看

第一个函数

void kernel_restart_prepare(char *cmd)
{
blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); 
system_state = SYSTEM_RESTART;
usermodehelper_disable();
device_shutdown();
}

看看

int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
unsigned long val, void *v)   //经典用法,void指针可以指向任何类型的指针,这里指向char*
{
return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
}


看看int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  unsigned long val, void *v,
  int nr_to_call, int *nr_calls)
{
int ret = NOTIFY_DONE;


/*
* We check the head outside the lock, but if this access is
* racy then it does not matter what the result of the test
* is, we re-check the list after having taken the lock anyway:
*/
if (rcu_dereference_raw(nh->head)) {
down_read(&nh->rwsem);
ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
nr_calls);
up_read(&nh->rwsem);
}
return ret;
}

看看ret = nb->notifier_call(nb, val, v);这里最后调用了driver里的notify函数

static struct notifier_block perf_reboot_notifier = {
.notifier_call = perf_reboot,
.priority = INT_MIN,
};类似这样的


这个struct notifier_block {
notifier_fn_t notifier_call;
struct notifier_block __rcu *next;
int priority;
};还用了函数指针

typedef int (*notifier_fn_t)(struct notifier_block *nb,
unsigned long action, void *data);  属于隐藏指针语法

1
typedef char* pstr;
1
int mystrcmp(const pstr p1,const pstr p3);
和这个相同


这里的BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);在notifier.c里定义了

怎么加入这个list呢,是下面这个函数

int register_reboot_notifier(struct notifier_block *nb)
{
return blocking_notifier_chain_register(&reboot_notifier_list, nb);
}这里涉及内核的notify机制,先掌握用法再看具体实现

void device_shutdown(void)
{
struct device *dev, *parent;


spin_lock(&devices_kset->list_lock);
/*
* Walk the devices list backward, shutting down each in turn.
* Beware that device unplug events may also start pulling
* devices offline, even as the system is shutting down.
*/
while (!list_empty(&devices_kset->list)) {
dev = list_entry(devices_kset->list.prev, struct device,
kobj.entry);


/*
* hold reference count of device's parent to
* prevent it from being freed because parent's
* lock is to be held
*/
parent = get_device(dev->parent);
get_device(dev);
/*
* Make sure the device is off the kset list, in the
* event that dev->*->shutdown() doesn't remove it.
*/
list_del_init(&dev->kobj.entry);
spin_unlock(&devices_kset->list_lock);


/* hold lock to avoid race with probe/release */
if (parent)
device_lock(parent);
device_lock(dev);


/* Don't allow any more runtime suspends */
pm_runtime_get_noresume(dev);
pm_runtime_barrier(dev);


if (dev->bus && dev->bus->shutdown) {   //调用bus的shut
if (initcall_debug)
dev_info(dev, "shutdown\n");
dev->bus->shutdown(dev);
} else if (dev->driver && dev->driver->shutdown) {//调用driver的shut
if (initcall_debug)
dev_info(dev, "shutdown\n");
dev->driver->shutdown(dev);
}


device_unlock(dev);
if (parent)
device_unlock(parent);


put_device(dev);
put_device(parent);


spin_lock(&devices_kset->list_lock);
}
spin_unlock(&devices_kset->list_lock);
async_synchronize_full();
}

第二个函数

static void migrate_to_reboot_cpu(void)
{
/* The boot cpu is always logical cpu 0 */
int cpu = 0;


cpu_hotplug_disable();


/* Make certain the cpu I'm about to reboot on is online */
if (!cpu_online(cpu))
cpu = cpumask_first(cpu_online_mask);


/* Prevent races with other tasks migrating this task */
current->flags |= PF_NO_SETAFFINITY;


/* Make certain I only run on the appropriate processor */
set_cpus_allowed_ptr(current, cpumask_of(cpu));
}

第三个函数

void syscore_shutdown(void)
{
struct syscore_ops *ops;


mutex_lock(&syscore_ops_lock);


list_for_each_entry_reverse(ops, &syscore_ops_list, node)       //static LIST_HEAD(syscore_ops_list); irq之类的在这个list里
if (ops->shutdown) {  //调用他们自身的shutdowm 这也是driver创建ops的用处
if (initcall_debug)
pr_info("PM: Calling %pF\n", ops->shutdown);
ops->shutdown();
}


mutex_unlock(&syscore_ops_lock);
}

第四个函数

void machine_restart(char *cmd)
{
/* Disable interrupts first */
local_irq_disable();
smp_send_stop();


/* Now call the architecture specific reboot code. */
if (arm_pm_restart)
arm_pm_restart(REBOOT_HARD, cmd);


/*
* Whoops - the architecture was unable to reboot.
*/
printk("Reboot failed -- System halted\n");
while (1);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值