kernel power off流程分析

本文深入探讨了Linux内核上层关机时的底层流程,从SYSCALL_DEFINE4到kernel_power_off,再到pm_power_off_prepare和pm_power_off,详细解析了各关键函数的作用及其与平台的关联性。通过高通平台的实例,展示了如何在特定平台上定制关机流程。此外,还介绍了reboot函数的实现细节,包括权限验证、魔法参数检查以及不同关机命令的处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

凡是linux内核上层关机时,底层均会调到kernel_power_off(),电脑可以使用按键ctr+alt+del键进入关机,下面我们看看代码流程:

 

SYSCALL_DEFINE4() -> kernel_power_off()-> pm_power_off_prepare() -> machine_power_off()->    pm_power_off()

 

在这里我想说的是pm_power_off_prepare()pm_power_off()都是与平台相关,有些平台只有填充pm_power_offpm_power_off_prepare并没有填充。而在pm_power_off()函数中一般函数不能被调用因为整个syscore已经关闭,系统不能调用idle thread。所以关机前我们通常在pm_power_off_prepare()函数来实现,关机前通常很有可能需要完成的其他事,我们在这里就可以填充。

 

 

/*

 * Function pointers to optional machine specific functions

 */

void (*pm_power_off)(void);

EXPORT_SYMBOL(pm_power_off);

/*

 * If set, this is used for preparing the system to power off.

 */

 

void (*pm_power_off_prepare)(void);

 

例如我们在高通平台所加的:

arch/arm/mach-msm/restart.c

 

static int __init msm_restart_init(void)

{

      pm_power_off = msm_power_off;

      pm_power_off_prepare = msm_power_off_prepare;

 

}

 

源代码:

Kernel/sys.c

SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,

             void __user *, arg)

{

      char buffer[256];

      int ret = 0;

 

      /* We only trust the superuser with rebooting the system. */

      if (!capable(CAP_SYS_BOOT))

             return -EPERM;

 

      /* For safety, we require "magic" arguments. */

      if (magic1 != LINUX_REBOOT_MAGIC1 ||

         (magic2 != LINUX_REBOOT_MAGIC2 &&

                     magic2 != LINUX_REBOOT_MAGIC2A &&

                    magic2 != LINUX_REBOOT_MAGIC2B &&

                     magic2 != LINUX_REBOOT_MAGIC2C))

             return -EINVAL;

 

      /* Instead of trying to make the power_off code look like

       * halt when pm_power_off is not set do it the easy way.

       */

      if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)

             cmd = LINUX_REBOOT_CMD_HALT;

 

      mutex_lock(&reboot_mutex);

      switch (cmd) {

      case LINUX_REBOOT_CMD_RESTART:

             kernel_restart(NULL);

             break;

 

      case LINUX_REBOOT_CMD_CAD_ON:

             C_A_D = 1;

             break;

 

      case LINUX_REBOOT_CMD_CAD_OFF:

             C_A_D = 0;

             break;

 

      case LINUX_REBOOT_CMD_HALT:

             kernel_halt();

             do_exit(0);

             panic("cannot halt");

 

      case LINUX_REBOOT_CMD_POWER_OFF:

             kernel_power_off();

             do_exit(0);

             break;

 

      case LINUX_REBOOT_CMD_RESTART2:

             if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {

                    ret = -EFAULT;

                    break;

             }

             buffer[sizeof(buffer) - 1] = '\0';

 

             kernel_restart(buffer);

             break;

 

#ifdef CONFIG_KEXEC

      case LINUX_REBOOT_CMD_KEXEC:

             ret = kernel_kexec();

             break;

#endif

 

#ifdef CONFIG_HIBERNATION

      case LINUX_REBOOT_CMD_SW_SUSPEND:

             ret = hibernate();

             break;

#endif

 

      default:

             ret = -EINVAL;

             break;

      }

      mutex_unlock(&reboot_mutex);

      return ret;

}

 

void kernel_power_off(void)

{

      kernel_shutdown_prepare(SYSTEM_POWER_OFF);

      if (pm_power_off_prepare)

             pm_power_off_prepare();

      disable_nonboot_cpus();

      syscore_shutdown();

      printk(KERN_EMERG "Power down.\n");

      kmsg_dump(KMSG_DUMP_POWEROFF);

      machine_power_off();

}

 

void machine_power_off(void)

{

      machine_shutdown();

      if (pm_power_off)

             pm_power_off();

}

05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result {"code":200,"msg":"操作成功","data":[{"id":1,"name":"华为Mate 60 Pro","model":"ALN-AL80","imageUrl":"https://img.alicdn.com/imgextra/i3/2213047591560/O1CN01QNx6eB1NOWu3oeUfn_!!2213047591560.jpg_q50.jpg_.webp","shortDescription":"麒麟9000S 卫星通信","price":6999.00,"subsidizedPrice":6499.00,"sales":15000,"rating":4.90,"stock":500,"isAvailable":0,"brand":"华为"},{"id":2,"name":"华为P60 Art","model":"NOH-AN80","imageUrl":"https://img.alicdn.com/imgextra/i4/2609173245/O1CN011h2L1I1ZqGCdlsYlJ_!!2609173245.jpg_q50.jpg_.webp","shortDescription":"超聚光XMAGE影像","price":7988.00,"subsidizedPrice":7588.00,"sales":12000,"rating":4.80,"stock":300,"isAvailable":0,"brand":"华为"},{"id":3,"name":"华为nova 12","model":"BAC-AL00","imageUrl":"https://picasso.alicdn.com/imgextra/O1CNA1bnbda21Vuba4TjLYB_!!2838892713-0-psf.jpg_.webp","shortDescription":"前置双摄人像","price":2999.00,"subsidizedPrice":2699.00,"sales":50000,"rating":4.70,"stock":1000,"isAvailable":0,"brand":"华为"},{"id":4,"name":"荣耀Magic6 Pro","model":"PGT-AN30","imageUrl":"https://img.alicdn.com/imgextra/i2/263726286/O1CN01ko93gt1wJ2hQAORAB_!!4611686018427380942-0-item_pic.jpg_.webp","shortDescription":"骁龙8 Gen3 青海湖电池","price":5699.00,"subsidizedPrice":5399.00,"sales":25000,"rating":4.80,"stock":600,"isAvailable":0,"brand":"荣耀"},{"id":5,"name":"荣耀X50 GT","model":"ANY-AN00","imageUrl":"https://img.alicdn.com/imgextra/i3/3851598352/O1CN01UHPfyv2BZGwPnHUlx_!!0-item_pic.jpg_q50.jpg_.webp","shortDescription":"1.5K护眼曲屏","price":1999.00,"subsidizedPrice":1799.00,"sales":80000,"rating":4.60,"stock":1500,"isAvailable":0,"brand":"荣耀"},{"id":6,"name":"荣耀90 Pro","model":"REA-AN20","imageUrl":"https://img.alicdn.com/imgextra/i3/1114511827/O1CN01r3ngr41PMof7MhFhY_!!4611686018427386323-0-item_pic.jpg_q50.jpg_.webp","shortDescription":"2亿像素写真相机","price":3299.00,"subsidizedPrice":2999.00,"sales":40000,"rating":4.70,"stock":800,"isAvailable":0,"brand":"荣耀"},{"id":7,"name":"荣耀Play7T Pro","model":"CMA-AN40","imageUrl":"https://img.alicdn.com/imgextra/i1/1907069314/O1CN019jd3kt2IfrnZ6J0AH_!!1907069314.jpg_q50.jpg_.webp","shortDescription":"40W快充 OLED屏","price":1799.00,"subsidizedPrice":1599.00,"sales":120000,"rating":4.50,"stock":2000,"isAvailable":0,"brand":"荣耀"},{"id":8,"name":"华为畅享70 Pro","model":"CMA-AN70","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"7000mAh长续航","price":1599.00,"subsidizedPrice":1399.00,"sales":150000,"rating":4.40,"stock":3000,"isAvailable":0,"brand":"华为"},{"id":9,"name":"华为Mate X5","model":"TET-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"折叠屏 玄武钢化","price":12999.00,"subsidizedPrice":12499.00,"sales":5000,"rating":4.90,"stock":100,"isAvailable":0,"brand":"华为"},{"id":10,"name":"荣耀Magic Vs2","model":"CMA-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"轻薄折叠屏","price":8999.00,"subsidizedPrice":8499.00,"sales":10000,"rating":4.70,"stock":300,"isAvailable":0,"brand":"荣耀"},{"id":11,"name":"iPhone 15 Pro Max","model":"A3108","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A17 Pro 钛金属","price":9999.00,"subsidizedPrice":null,"sales":80000,"rating":4.80,"stock":200,"isAvailable":0,"brand":"苹果"},{"id":12,"name":"iPhone 15","model":"A3092","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A16芯片 灵动岛","price":5999.00,"subsidizedPrice":null,"sales":150000,"rating":4.80,"stock":5000,"isAvailable":0,"brand":"苹果"},{"id":13,"name":"iPhone 15 Plus","model":"A3096","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"6.7英寸大屏","price":6999.00,"subsidizedPrice":null,"sales":80000,"rating":4.70,"stock":3000,"isAvailable":0,"brand":"苹果"},{"id":14,"name":"iPhone SE 4","model":"A2785","imageUrl":"https://android-api.oss-cn-beijing.aliyun 05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result [object Object] 05-23 16:00:49.359 25132-25512 C03f00/ArkCompiler E [ArkRuntime Log] TypeError: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Lifetime: 0.000000s 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Js-Engine: ark 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]page: pages/Index.js 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Error message: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Stacktrace: 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at forEachUpdateFunction (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4352:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:144:13) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateFunc (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6812:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at UpdateElement (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6514:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6741:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateDirtyElements (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6736:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at rerender (entry|entry|1.0.0|src/main/ets/pages/Index.ts:152:9)
05-24
### 关于 HarmonyOS 或 ArkCompiler 中 'is not callable' 的 TypeError 问题 在 HarmonyOS 和 ArkCompiler 开发环境中遇到 `TypeError: is not callable` 错误通常表明尝试调用的对象并非函数或可调用对象。这种错误可能由多种原因引起,例如变量名冲突、未正确定义的回调函数或者对 `null` 值进行了不当操作。 #### 可能的原因分析 1. **变量覆盖了内置方法或函数** 如果开发者无意间将某些全局名称重新赋值为其他类型的值(如 `null`),可能会导致该错误。例如,在 JavaScript 环境下,如果重写了 `Array.prototype.map` 方法并将其设置为 `null`,那么后续对该方法的调用会失败[^4]。 2. **异步编程中的上下文丢失** 在使用箭头函数或其他形式绑定事件处理器时,如果没有正确处理 this 上下文,则可能导致原本期望作为函数执行的内容变成了 undefined 或 null。这种情况常见于 React 组件生命周期方法以及 Vue.js 自定义指令场景中[^5]。 3. **API 返回值异常** 当从外部服务获取数据时,若返回的结果不符合预期结构——比如应该是一个函数却实际接收到的是 null ——也会引发此类错误消息提示[^6]。 4. **Null 安全性问题** 类似于 MySQL 数据库设计里提到允许字段为空 (NULL)[^2], 在程序逻辑层面也需要考虑如何优雅地应对可能出现的 Null 情况。如果不小心试图直接调用了被赋予 Null 的属性或方法就会抛出类似的运行期错误。 #### 解决方案建议 针对以上几种可能性提供相应的解决方案如下: - #### 验证目标是否确实具备可调用特性 在每次准备调用前先验证其类型是否属于 Function 。可以利用 typeof 运算符来进行初步判断。 ```javascript const maybeFunction = someCondition ? () => {} : null; if(typeof maybeFunction === 'function') { maybeFunction(); } ``` - #### 使用 Optional Chaining 提高健壮性 ES2020 引入了一个新功能叫做 optional chaining (`?.`) ,它可以帮助我们更简洁安全地访问深层嵌套对象而不用担心中间环节存在 null/undefined 导致崩溃风险。 ```javascript let result = possiblyUndefinedObject ?. methodToCall(); // 不会报错即使object不存在 ``` - #### 明确区分命名空间内的同名实体 尽量避免自定义标识符与框架内部保留字发生碰撞;另外通过模块化组织代码也能有效减少这类隐患的发生几率。 - #### 调试工具辅助定位具体位置 利用断点调试技术逐步跟踪哪一部分代码最终触发了这个特定异常,并针对性修复源头问题所在之处。 ```javascript try{ potentiallyProblematicCode(); }catch(e){ console.error('Error occurred:', e); } ``` ### 总结 通过对 HarmonyOS / ArkCompiler 平台开发过程中常见的 “not callable” 类型错误现象剖析可知,这往往涉及到基本概念理解偏差或者是编码习惯上的疏漏所致。采取适当预防措施加上良好实践能够显著降低遭遇这些问题的概率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值