1.. include:: ../disclaimer-zh_CN.rst
2
3:Original: Documentation/kernel-hacking/hacking.rst
4
5:������:
6
7 ��������� Wu XiangCheng <bobwxc@email.cn>
8
9==============
10������������������
11==============
12
13:������: Rusty Russell
14
15������
16=====
17
18������������������������������������Rusty���������������������Linux���������������Hacking������������������
19������������������������������������������������������������������������������������C���������������Linux������
20������������������������������������������������������������������������������������������������
21
22���������������������������������������������������������������������������������������������������������������
23���������������������������������������������������������������������������������������������������������������
24���������������������������������������
25
26������
27=======
28
29������������������������������������CPU������������
30
31-  ������������������������������������������������
32
33-  ������������������������������������������������softirq������������������tasklet������
34
35-  ���������������������������������������������������������������������
36
37-  ���������������������������������
38
39������������������������������������������������������������������������������������������������������������
40���������������������������������������������������������������������CPU������������������������������������
41������������������������������������������������������������������������������������CPU������������������������
42
43���������������������������������������������������������������������������������������������������������
44
45���������������
46------------
47
48���������������������������������������������������������������������������������������������������������������
49������������������������������������������������������ :c:func:`schedule()` ���������������
50
51.. note::
52
53    ������������������������������������������������������������������������������������������������
54
55������������������������������ ``current`` ������������������������������������������������������������������
56��� :c:func:`in_interrupt()` ��� ``include/linux/preempt.h`` ���������������false������
57
58.. warning::
59
60    ������������������������������������������������������������������:c:func:`in_interrupt()` ���
61    ������������������
62
63���������������Hard IRQs���
64----------------------
65
66������������������������������������������������������������������������������������������������������������
67������������������������������������������������������������������������������������������������������������������
68������������������������������������������������������������������������������������������������������������
69������������������������������������������������������������������
70
71��������������� in_hardirq() ������������������������������������������������
72
73.. warning::
74
75    ���������������������������������������������������������������������������
76
77������������������������������������Softirqs������������������Tasklets���
78-------------------------------------------------------
79
80���������������������������������������������������������������������������������������������������������������
81��������������������������������������������������� ``kernel/softirq.c`` ������
82
83���������������������������������������������������������SMP���������������������������bottom halves������
84���������BHs������������������������������CPU���������������������������������������������������������������������
85������������������������������������������������������������
86
87``include/linux/interrupt.h`` ���������������������������������������������������������������������
88��������������� ``include/linux/timer.h`` ���������������������������������������������������������
89���������
90
91������������������������������������������������������������������������������������������CPU���������������������
92������������ ``include/linux/interrupt.h`` ������������������������������������������������������
93���������������������������������������������������������������������������������������CPU���������������������
94���������������������������������
95
96.. warning::
97
98    ���tasklet���������������������������������������������������������������
99
100��������������� :c:func:`in_softirq()` ������ ``include/linux/preempt.h`` ������������
101���������������������������������������������
102
103.. warning::
104
105    ��������������������� :ref:`bottom half lock <local_bh_disable_zh>` ������������������
106    ������������
107
108������������������
109================
110
111������������������
112    ������������������������������������������������������������������������������������������������������������
113    ������������������������������������������������������������
114
115���������������MMX
116    FPU���������������������������������������������������������FPU������������������������������������������
117    ���������������������������������FPU������������������������������������������������������������/������
118    ���������FPU������������������������������������������������������������������������������������������������
119
120���������������������
121    ���������������32������������������������������������������������������������������3K���6K������������
122    ������64���������������������������������14K���������������������������������������������������������������
123    ������������������������������������������������������������������������������������������
124
125Linux���������������������
126    ���������������������������������������64���������������������������������������endian������������������
127    ������������CPU���������������������������������������inline assembly������������������������������
128    ���������������������������������������������������������������������������������������������������������
129
130���������������������ioctls������������������������������������
131==============================================
132
133���������������system call���������������������������::
134
135    asmlinkage long sys_mycall(int arg)
136    {
137            return 0;
138    }
139
140
141������������������������������������������������������������������������������������������������������������������
142������������������������ioctls������������������������������������������������������������������������
143``include/asm/unistd.h`` ��� ``arch/kernel/entry.S`` ������������������������������Linus
144���������
145
146������������������������������������������������������������������������������ :c:func:`sysfs()` ���������
147
148���������������������������������������������������������������������������������������������������������������
149���errno������������ ``include/uapi/asm-generic/errno-base.h`` ���
150``include/uapi/asm-generic/errno.h`` ��� ``include/linux/errno.h`` ���������������
151���0���
152
153���������������������������������������������������������Unix/Linux������������������������������������������
154������������������ ``-ERESTARTSYS`` ���������������������������������������������������������������������
155������������������������������������������������������������������������������������������������������������������
156���������������������������������������������������������������������������
157
158::
159
160    if (signal_pending(current))
161            return -ERESTARTSYS;
162
163
164������������������������������������������������������������������������������������������������������������������
165���������������������������������������CPU������������������CPU���������������������������
166������������::
167
168    cond_resched(); /* Will sleep */
169
170
171���������������������������UNIX������������������������������������������������������
172Provide mechanism not policy������
173
174���������������������
175====================
176
177������������������������������������������������������
178
179- ������������������������������
180
181- ������������������������������
182
183- ������������������������������������Andi Kleen���������������������������������������������������������
184  ������������������
185
186������������������������������������������������������������������������������������\*_user������������
187``GFP_ATOMIC`` ������������������������
188
189���������������������  ``CONFIG_DEBUG_ATOMIC_SLEEP``  ������������������������������������������
190������������������������������������ **������** ���������������������������������������������������
191
192������������������
193
194
195������������/������
196===============
197
198:c:func:`printk()`
199------------------
200
201��������� ``include/linux/printk.h``
202
203:c:func:`printk()` ������������������������������������dmesg���syslog���������������������������
204���������������������������������������������������������������������������������������������������������������
205������������������������printk���������������������������������������ANSI C printf���������������������
206���������������������C������������������������������������������������������::
207
208    printk(KERN_INFO "i = %u\n", i);
209
210
211������ ``include/linux/kern_levels.h`` ��������������� ``KERN_`` ������syslog������������
212���������������������������������������IP������������::
213
214    __be32 ipaddress;
215    printk(KERN_INFO "my ip: %pI4\n", &ipaddress);
216
217
218:c:func:`printk()` ���������������1K������������������������������������������������������
219
220.. note::
221
222    ���������������������������������printf������printk���������������������������������������������������
223    :)
224
225.. note::
226
227    ���������������������������unix������������������������printf������������������������������������printf
228    ���������������������������������������������������������������
229
230:c:func:`copy_to_user()` / :c:func:`copy_from_user()` / :c:func:`get_user()` / :c:func:`put_user()`
231---------------------------------------------------------------------------------------------------
232
233��������� ``include/linux/uaccess.h`` / ``asm/uaccess.h``
234
235**[������]**
236
237:c:func:`put_user()` ��� :c:func:`get_user()` ���������������������������������������������
238���������������������������int���char���long������������������������������������������������������������
239��������������������������������������������������������������� ``-EFAULT`` ��� 0���
240
241:c:func:`copy_to_user()` ��� :c:func:`copy_from_user()` ���������������������/���������
242������������������������������������
243
244.. warning::
245
246    ��� :c:func:`put_user()` ��� :c:func:`get_user()` ���������������������������������
247    ���������������0���������������������������
248
249���������������������������������������������������������������������������������������������������
250������ Rusty Russell���
251
252���������������������������������������������������������������������������������������������������������������������
253���������������������
254
255:c:func:`kmalloc()`/:c:func:`kfree()`
256-------------------------------------
257
258��������� ``include/linux/slab.h``
259
260**[���������������������]**
261
262���������������������������������������������������������������������������������malloc���free������
263:c:func:`kmalloc()` ������������������������������������������
264
265``GFP_KERNEL``
266    ������������������������������������������������������������������������������������������������������������
267    ������������
268
269``GFP_ATOMIC``
270    ������������������ ``GFP_KERNEL`` ������������������������������������������������������ **������**
271    ���������������������������������������������������
272
273``GFP_DMA``
274    ������������16MB���ISA DMA������������������������������������������������������������������������������
275
276������������������������������������������������������������������������������������������������������
277``GFP_ATOMIC`` ���������������������������������������������������������������������������������������������
278���������
279
280������������������������ ``PAGE_SIZE`` ��� ``asm/page.h`` ��� ``asm/page_types.h`` ���
281������������������������ :c:func:`__get_free_pages()` ��� ``include/linux/gfp.h`` ������
282������������������������0���������������������1���������������2������������������������������������������������
283���������������������
284
285��������������������������������������������������� :c:func:`vmalloc()` ������������������������������
286������������������������������������������������������������������MMU������������������������������������������
287���������������������������������������������������������������cpu���������������������������������������������������
288������������������������������������������������������������������������������������������������������������������
289Linux���������������������������������������������������������������������������������������������������������
290��������������������������������������� :c:func:`alloc_bootmem()` ���������������
291
292������������������������������������������������������������ ``include/linux/slab.h`` ������slab
293���������
294
295:c:macro:`current`
296------------------
297
298��������� ``include/asm/current.h``
299
300������������������������������������������������������������������task structure���������������������������
301���������������������������������������������������������������������������������������������������������������
302���������������������������������**not NULL**������
303
304:c:func:`mdelay()`/:c:func:`udelay()`
305-------------------------------------
306
307��������� ``include/asm/delay.h`` / ``include/linux/delay.h``
308
309:c:func:`udelay()` ��� :c:func:`ndelay()` ���������������������������������������������������
310��������������������������������������������������������� :c:func:`mdelay()` ���������������������������
311������ :c:func:`msleep()`���
312
313:c:func:`cpu_to_be32()`/:c:func:`be32_to_cpu()`/:c:func:`cpu_to_le32()`/:c:func:`le32_to_cpu()`
314-----------------------------------------------------------------------------------------------
315
316��������� ``include/asm/byteorder.h``
317
318:c:func:`cpu_to_be32()` ������������������������32������������������64���16������be������������������
319���le������������������������������������������������������������������������������������������������������������
320���������������������������
321:c:func:`be32_to_cpu()` ������
322
323������������������������������������������������������������ :c:func:`cpu_to_be32p()` ������������
324������������������������������������������������������������������������������in-situ������������������
325:c:func:`cpu_to_be32s()` ������������������������������������������void���
326
327:c:func:`local_irq_save()`/:c:func:`local_irq_restore()`
328--------------------------------------------------------
329
330��������� ``include/linux/irqflags.h``
331
332
333������������������������CPU������������������������������������������������������������������������
334``unsigned long flags`` ���������������������������������������������������������������������������
335������������ :c:func:`local_irq_disable()` ��� :c:func:`local_irq_enable()`���
336
337.. _local_bh_disable_zh:
338
339:c:func:`local_bh_disable()`/:c:func:`local_bh_enable()`
340--------------------------------------------------------
341
342��������� ``include/linux/bottom_half.h``
343
344
345������������������������CPU���������������������������������������������������������������������������������
346���������������������������������������������������������������������������������������������������������������
347CPU������������
348
349:c:func:`smp_processor_id()`
350----------------------------
351
352��������� ``include/linux/smp.h``
353
354:c:func:`get_cpu()` ���������������������������������������������������������cpu������������������
355���������������������0��� ``NR_CPUS`` ���������������������CPU������������������������������������������
356������ :c:func:`put_cpu()` ���������������
357
358���������������������������������������������������������������������������������������������������������������
359������������ :c:func:`smp_processor_id()`���
360
361``__init``/``__exit``/``__initdata``
362------------------------------------
363
364���������  ``include/linux/init.h``
365
366������������������������������������������������������ ``__init`` ��������������������� ``__initdata``
367������������������������������������������������������������������������������������������������������
368``__exit`` ������������������������������������������������������������������������������������������������
369������������������������������������������������������������ :c:func:`EXPORT_SYMBOL()` ���
370:c:func:`EXPORT_SYMBOL_GPL()` ������������ ``__init`` ���������������������������������������
371���������������������������
372
373
374:c:func:`__initcall()`/:c:func:`module_init()`
375----------------------------------------------
376
377���������  ``include/linux/init.h`` / ``include/linux/module.h``
378
379���������������������������������������������������������������������������������������������
380:c:func:`module_init()` ��� :c:func:`module_exit()` ������������������������������������
381``#ifdef`` ������������������������������������������������������
382
383:c:func:`module_init()` ���������������������������������������������������������������������������
384������������������������������������������������������:c:func:`module_init()` ���������������
385:c:func:`__initcall()` ������������������������������������������������������������������
386
387������������������������������������������������������������������������������������������������������������������
388���������������������������������������������������������������������������������������������������������
389
390:c:func:`module_exit()`
391-----------------------
392
393
394���������  ``include/linux/module.h``
395
396������������������������������������������������������������������������������������������������������������������������
397������������������������������������������������������������������������������������������������������������������
398���������������������������������������������
399
400������������������������������������������������������������������������������������������ ``rmmod -f`` ������
401
402:c:func:`try_module_get()`/:c:func:`module_put()`
403-------------------------------------------------
404
405��������� ``include/linux/module.h``
406
407������������������������������������������������������������������������������������������������������������������
408������������������������������������������������������������������������������������������������������
409:c:func:`try_module_get()` ������������������������������������������������������������������������������
410��������������������������������������������������������������������������� :c:func:`module_put()` ���
411
412������������������������������������������������������������
413:c:type:`struct file_operations <file_operations>` ������������������������������������
414��� ``THIS_MODULE`` ���
415
416������������ ``include/linux/wait.h``
417====================================
418
419**[������]**
420
421������������������������������������������������������������������������������������������������������������������
422������������������������ :c:type:`wait_queue_head_t` ������������������������������������������������
423��������������������������� :c:type:`wait_queue_entry_t` ������������������������������
424
425������
426-----
427
428������ :c:func:`DECLARE_WAIT_QUEUE_HEAD()` ��������������� ``wait_queue_head_t`` ���
429��������������������������������� :c:func:`init_waitqueue_head()` ���������
430
431������
432-----
433
434������������������������������������������������������������������������������������������������������������������
435��������������������������������� :c:func:`wait_event_interruptible()`
436��� ``include/linux/wait.h`` ������������������������������������������������������������������������
437������������������������true������������0������������������������������������ ``-ERESTARTSYS`` ���
438:c:func:`wait_event()` ������������������������
439
440������������������
441-------------
442
443������ :c:func:`wake_up()` ��� ``include/linux/wait.h`` ������������������������������������
444������������������������������������������������������ ``TASK_EXCLUSIVE`` ������������������������������
445������������������������������������������������������������������������������������������
446
447������������
448=========
449
450��������������������������������������������������������������� :c:type:`atomic_t`
451��� ``include/asm/atomic.h`` ������������������������������������������������������32������������
452��������������������������������������������� :c:type:`atomic_t` ���������
453:c:func:`atomic_read()` ��� :c:func:`atomic_set()` ���������������������������������
454:c:func:`atomic_add()` ���:c:func:`atomic_sub()` ���:c:func:`atomic_inc()` ���
455:c:func:`atomic_dec()` ��� :c:func:`atomic_dec_and_test()` ������������������������
456���������true������
457
458������������������������������������������true������!=0������
459
460���������������������������������������������������������������������������������������
461
462��������������������������� ``unsigned long`` ��� ``include/linux/bitops.h`` ���������
463������������������������������������������������������������bit pattern������������������0������������������
464������:c:func:`set_bit()`���:c:func:`clear_bit()` ��� :c:func:`change_bit()` ���������
465���������������������������:c:func:`test_and_set_bit()` ���:c:func:`test_and_clear_bit()`
466��� :c:func:`test_and_change_bit()` ���������������������������������������������������������������
467true������������������������������������������������
468
469������������������ ``BITS_PER_LONG`` ������������������������������������������������������������������
470���������������������������������������������
471
472������
473=====
474
475������������������������������������������������������������������static������������������������������������������
476���������������������������������������������������������������������������������������������������������������������
477������������������������������������������������������������������������
478
479:c:func:`EXPORT_SYMBOL()`
480-------------------------
481
482��������� ``include/linux/export.h``
483
484���������������������������������������������������������������������������������������
485
486:c:func:`EXPORT_SYMBOL_GPL()`
487-----------------------------
488
489��������� ``include/linux/export.h``
490
491
492��������� :c:func:`EXPORT_SYMBOL()`��������� :c:func:`EXPORT_SYMBOL_GPL()` ���������
493������������������������ :c:func:`MODULE_LICENSE()` ������GPL������������������������������������
494���������������������������������������������������������������������������������������������������������������
495���������������������������������API��������������������������������� EXPORT_SYMBOL_GPL()���
496
497:c:func:`EXPORT_SYMBOL_NS()`
498----------------------------
499
500��������� ``include/linux/export.h``
501
502������ ``EXPORT_SYMBOL()`` ������������������������������������������������������������������������
503Documentation/core-api/symbol-namespaces.rst ���
504
505:c:func:`EXPORT_SYMBOL_NS_GPL()`
506--------------------------------
507
508��������� ``include/linux/export.h``
509
510������ ``EXPORT_SYMBOL_GPL()`` ������������������������������������������������������������������������
511Documentation/core-api/symbol-namespaces.rst ���
512
513���������������
514===========
515
516������������ ``include/linux/list.h``
517-----------------------------------
518
519������������������������������������������������������������������������������������������������������������������������
520���������������������������������������������
521
522������ :c:func:`list_for_each_entry()` ������������
523
524���������������
525------------
526
527���������������������������������������������������C���������������������������������������0���������������������
528��������������������� ``-EFAULT`` ������������������������������������������������������������������������
529���������������
530
531������ :c:func:`ERR_PTR()` ��� ``include/linux/err.h`` ���������������������������������������
532������������ :c:func:`IS_ERR()` ��� :c:func:`PTR_ERR()` ������������������������������������
533������������������������������������������������������������������������
534
535������������
536----------
537
538Linus���������������������������������������������������������������������������������������������������������
539���������������������������������������������������������������������������������������������������������������
540���������������������������������������������������������������������������������������������������������������
541������������������������������������������������������������������������������������������������������������������
542��������������������������������� **������** ���
543
544������������������������
545------------------
546
547���������������������������������������������������������������������ISO C99���������
548������::
549
550    static struct block_device_operations opt_fops = {
551            .open               = opt_open,
552            .release            = opt_release,
553            .ioctl              = opt_ioctl,
554            .check_media_change = opt_media_change,
555    };
556
557
558���������������������������grep������������������������������������������������������������������������������������
559���������������������������
560
561GNU ������
562----------
563
564Linux���������������������GNU������������������������������������������������������������������������������
565���������������������������������������������������������������������������������������������������GCC info���
566������C ������������������������������������������info���������������������info������������������������������
567
568- ������������
569
570- ������������������Statement expressions������������{ ��� }���������������
571
572
573- ������������/������/������������������__attribute__���
574
575- typeof
576
577- ���������������
578
579- ���������
580
581- ���������������
582
583- ������������Non-Constant������������������
584
585- ������������������������ arch/ ��� include/asm/ ���������
586
587- ���������������������__func__������
588
589- __builtin_constant_p()
590
591������������������long long���������������gcc������������������������������������������������������i386���
592������������������������������������������������������gcc������������������
593
594C++
595---
596
597������������������C++���������������������������������������������������������������������������������������
598������������������������������������������������������������������������������������������������������������
599���������������exceptions������
600
601#if
602---
603
604���������������������������������.c���������������������������������������������������������������������if������
605���������������������������
606
607������������������������������
608======================
609
610������������������������������������������������������������������������������
611
612-  ��������������������������������������������������������������������� ``MAINTAINERS`` ������������
613   ``CREDITS`` ������������������������������������������������������������������������������������������
614   ���������������������������������������������
615
616   ������������������������������������������������������������������������������������������������������������
617   ������������������������������������������������������������������������������������
618
619-  ������������������������������������������������������������������������������������������ ``Kconfig`` ���
620   ���������������������������������������������������������
621   Documentation/kbuild/kconfig-language.rst ������������������������
622
623   ������������������������������������������������������������������������������������������������������������
624   ������������������������������������������������������������������������������������N������������������Y���������
625   ������������������������������������������������������
626
627-  ������ ``Makefile`` ������������������������������������������������������������������
628   ���obj-$(CONFIG_xxx) += xxx.o���������������������
629   Documentation/kbuild/makefiles.rst ���
630
631-  ��������������������������������������������������������������������������� ``CREDITS`` ������������
632   ������������������������������������������������������������������������������  ``MAINTAINERS``
633   ���������������������������������������������������������������������������������������������������������
634   ���������������������������
635
636-  ��������������������������� Documentation/process/submitting-patches.rst���
637
638Kernel ���������
639===============
640
641������������������������������������������������������������������
642
643``arch/x86/include/asm/delay.h``::
644
645    #define ndelay(n) (__builtin_constant_p(n) ? \
646            ((n) > 20000 ? __bad_ndelay() : __const_udelay((n) * 5ul)) : \
647            __ndelay(n))
648
649
650``include/linux/fs.h``::
651
652    /*
653     * Kernel pointers have redundant information, so we can use a
654     * scheme where we can return either an error code or a dentry
655     * pointer with the same return value.
656     *
657     * This should be a per-architecture thing, to allow different
658     * error and pointer decisions.
659     */
660     #define ERR_PTR(err)    ((void *)((long)(err)))
661     #define PTR_ERR(ptr)    ((long)(ptr))
662     #define IS_ERR(ptr)     ((unsigned long)(ptr) > (unsigned long)(-1000))
663
664``arch/x86/include/asm/uaccess_32.h:``::
665
666    #define copy_to_user(to,from,n)                         \
667            (__builtin_constant_p(n) ?                      \
668             __constant_copy_to_user((to),(from),(n)) :     \
669             __generic_copy_to_user((to),(from),(n)))
670
671
672``arch/sparc/kernel/head.S:``::
673
674    /*
675     * Sun people can't spell worth damn. "compatibility" indeed.
676     * At least we *know* we can't spell, and use a spell-checker.
677     */
678
679    /* Uh, actually Linus it is I who cannot spell. Too much murky
680     * Sparc assembly will do this to ya.
681     */
682    C_LABEL(cputypvar):
683            .asciz "compatibility"
684
685    /* Tested on SS-5, SS-10. Probably someone at Sun applied a spell-checker. */
686            .align 4
687    C_LABEL(cputypvar_sun4m):
688            .asciz "compatible"
689
690
691``arch/sparc/lib/checksum.S:``::
692
693            /* Sun, you just can't beat me, you just can't.  Stop trying,
694             * give up.  I'm serious, I am going to kick the living shit
695             * out of you, game over, lights out.
696             */
697
698
699������
700=====
701
702������Andi Kleen���������������������������������������������������������������������������������
703������Philipp Rumpf������������������������������������������������������������������������������
704������Werner Almesberger��� :c:func:`disable_irq()` ������������������������������
705Jes Sorensen���Andrea Arcangeli������������������������������
706������Michael Elizabeth Chastain���������������������������������
707������Telsa Gwynne������DocBook���
708