2015年01月22日
情報科学類 オペレーティングシステム II
筑波大学 システム情報工学研究科
コンピュータサイエンス専攻, 電子・情報工学系
新城 靖
<yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/os2-2014/2015-01-22
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
/dev 以下のファイルをアクセスする。
$ df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 232431456 13088380 207345736 6% /
$ ls -l /dev/sda2
brw-r----- 1 root disk 8, 2 Jan 24 12:00 /dev/sda2
$
ls -l で見ると、ブロック型は、b、文字型は、c で始まる。メ
ジャー番号は、デバイスの種類、マイナー番号は、同じ種類で、細かい違い
(上の例では、パーティション)等を意味する。
メジャー番号は、静的に決めうちにすることもあるが、
alloc_chrdev_region() を呼び、動的に割り当てられることもできる。
使われているメジャー番号は、/proc/devices に現れる。
/dev/ の下にあるブロック型と文字型のファイルは、mknod コマンド (make
node) で作ることができる。
# mknod b /dev/ファイル名 メジャー番号 マイナー番号
# mknod c /dev/ファイル名 メジャー番号 マイナー番号
最近の
Linux では、起動時に自動的に mknod が行われるので、手で mknod コマンド
を打つ必要性はあまりない。
struct file_operations my_fops = { .... };
struct cdev *my_cdevp = cdev_alloc();
my_cdev->ops = &my_fops;
my_cdev->owner = &my_fops;
struct file_operations my_fops = { .... };
struct cdev my_cdev ;
cdev_init(&my_cdev,&my_fops);
cdev_add(&my_cdev,num, count)
register_chrdev() という関数で登録することもできる。以前はこの方法が主。
int register_chrdev(unsigned int major, const char *name,
struct file_operations *fops);
linux-3.18.1/include/linux/fs.h
1486: struct file_operations {
1487: struct module *owner;
1488: loff_t (*llseek) (struct file *, loff_t, int);
1489: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1490: ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1491: ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1492: ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1493: ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1494: ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
1495: int (*iterate) (struct file *, struct dir_context *);
1496: unsigned int (*poll) (struct file *, struct poll_table_struct *);
1497: long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1498: long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1499: int (*mmap) (struct file *, struct vm_area_struct *);
1500: int (*open) (struct inode *, struct file *);
1501: int (*flush) (struct file *, fl_owner_t id);
1502: int (*release) (struct inode *, struct file *);
1503: int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1504: int (*aio_fsync) (struct kiocb *, int datasync);
1505: int (*fasync) (int, struct file *, int);
1506: int (*lock) (struct file *, int, struct file_lock *);
1507: ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1508: unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1509: int (*check_flags)(int);
1510: int (*flock) (struct file *, int, struct file_lock *);
1511: ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1512: ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1513: int (*setlease)(struct file *, long, struct file_lock **, void **);
1514: long (*fallocate)(struct file *file, int mode, loff_t offset,
1515: loff_t len);
1516: int (*show_fdinfo)(struct seq_file *m, struct file *f);
1517: };
主な手続きの意味
デバイス・ファイルでは、open() 等で自分のメジャー番号とマイナー番号を取 り出すために使われることがある。
int fd1 = open("file1");
int fd2 = open("file1");
ファイル名 "file1" で表現されるファイルの inode 構造体は、1 個でも、
file 構造体は、2 個割り当てられる。
例
int ioctl(int d, int request, ...);
# date
Tue Jan 20 17:18:53 JST 2015
# hwclock --show
Tue Jan 20 08:18:56 2015 -0.656797 seconds
#
1:
2: /*
3: ~yas/syspro/time/rtc-read-time.c -- Read CMOS Realtime Clock in Linux
4: Created on: 2011/01/28 17:12:36
5: */
6:
7:
8: #include <sys/types.h> /* open() */
9: #include <sys/stat.h> /* open() */
10: #include <fcntl.h> /* open() */
11: #include <sys/ioctl.h> /* ioctl() */
12: #include <unistd.h> /* close() */
13: #include <stdio.h> /* printf() */
14: #include <stdlib.h> /* exit() */
15: #include <linux/rtc.h> /* RTC_RD_TIME */
16:
17: #define RTC_DEVICE_FILE "/dev/rtc"
18:
19: main()
20: {
21: int fd;
22: struct rtc_time t1 ;
23: if( (fd = open( RTC_DEVICE_FILE, O_RDONLY ))< 0 )
24: {
25: perror("open");
26: exit( 1 );
27: }
28: if( ioctl( fd, RTC_RD_TIME, &t1 ) < 0 )
29: {
30: perror("ioctl(RTC_RD_TIME)");
31: exit( 2 );
32: }
33: printf("%04d-%02d-%02d %02d:%02d:%02d\n",
34: t1.tm_year+1900, t1.tm_mon+1, t1.tm_mday,
35: t1.tm_hour, t1.tm_min, t1.tm_sec );
36: close( fd );
37: }
$ ls -l /dev/rtc
crw-r--r-- 1 root root 10, 135 Oct 29 18:22 /dev/rtc
$
$ rm rtc-read-time
$ make rtc-read-time
cc rtc-read-time.c -o rtc-read-time
$ su
Password:
# ./rtc-read-time
2015-01-20 08:24:24
# ./rtc-read-time ; date; hwclock --show
2015-01-20 08:24:26
Tue Jan 20 17:24:26 JST 2015
Tue Jan 20 08:24:27 2015 -0.906741 seconds
#
RTC_RD_TIME を含めて、/dev/rtc に対する ioctl() では、次のようなコマン
ドが使える。詳しくは、man rtc を参照。
| コマンド | 説明 |
|---|---|
| RTC_RD_TIME | RTCのTODを読む(read) |
| RTC_SET_TIME | RTCのTODに値をセットする |
| RTC_ALM_READ,RTC_ALM_SET | RTCのalarmを読む/セットする |
| RTC_IRQP_READ | alarmによる定期的な割り込みの(periodic interrupt)の周波数を読む/セットする |
| RTC_AIE_ON, RTC_AIE_OFF | alarmの割り込みを許可する/禁止する |
| RTC_UIE_ON, RTC_UIE_OFF | clockの更新後との割り込みを許可する/禁止する |
| RTC_PIE_ON, RTC_PIE_OFF | 定期的な割り込みを許可する/禁止する |
| RTC_EPOCH_READ, RTC_EPOCH_SET | RTCのepoch (起点となる年月日) を読む/書く |
linux-3.18.1/drivers/char/rtc.c
899: static const struct file_operations rtc_fops = {
900: .owner = THIS_MODULE,
901: .llseek = no_llseek,
902: .read = rtc_read,
903: #ifdef RTC_IRQ
904: .poll = rtc_poll,
905: #endif
906: .unlocked_ioctl = rtc_ioctl,
907: .open = rtc_open,
908: .release = rtc_release,
909: .fasync = rtc_fasync,
910: };
911:
912: static struct miscdevice rtc_dev = {
913: .minor = RTC_MINOR,
914: .name = "rtc",
915: .fops = &rtc_fops,
916: };
...
930: static struct resource * __init rtc_request_region(resource_size_t size)
931: {
...
1057: if (misc_register(&rtc_dev)) {
...
1064: return -ENODEV;
1065: }
...
1131: (void) init_sysctl();
1132:
1133: printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1134:
1135: return 0;
1136: }
linux-3.18.1/include/uapi/linux/major.h
11: #define RAMDISK_MAJOR 1
12: #define FLOPPY_MAJOR 2
13: #define PTY_MASTER_MAJOR 2
14: #define IDE0_MAJOR 3
15: #define HD_MAJOR IDE0_MAJOR
16: #define PTY_SLAVE_MAJOR 3
17: #define TTY_MAJOR 4
18: #define TTYAUX_MAJOR 5
19: #define LP_MAJOR 6
20: #define VCS_MAJOR 7
21: #define LOOP_MAJOR 7
22: #define SCSI_DISK0_MAJOR 8
23: #define SCSI_TAPE_MAJOR 9
24: #define MD_MAJOR 9
25: #define MISC_MAJOR 10
linux-3.18.1/include/linux/miscdevice.h
13: #define PSMOUSE_MINOR 1
14: #define MS_BUSMOUSE_MINOR 2 /* unused */
...
24: #define RTC_MINOR 135
$ dmesg | grep Real
Real Time Clock Driver v1.12ac
$
dmesg コマンドの古いものは、(syslogd の働きで) /var/log/messages* 等の
ファイルに保存される。
# grep Real /var/log/messages.2
...
Jan 24 12:00:40 windell50 kernel: Real Time Clock Driver v1.12ac
#
2015年
coins で動いている Linux では、別のドライバが動いている。
ソースは、drivers/rtc/rtc-cmos.c にある。
$ date
Tue Jan 20 17:50:27 JST 2015
$ dmesg | grep Real
$ dmesg | grep cmos
rtc_cmos 00:06: RTC can wake from S4
rtc_cmos 00:06: rtc core: registered rtc_cmos as rtc0
rtc_cmos 00:06: setting system clock to 2015-01-20 08:13:22 UTC (1421741602)
$ hostname
crocus09
$
linux-3.18.1/drivers/char/rtc.c
191: static unsigned long rtc_status; /* bitmapped status byte. */
730: static int rtc_open(struct inode *inode, struct file *file)
731: {
732: spin_lock_irq(&rtc_lock);
733:
734: if (rtc_status & RTC_IS_OPEN)
735: goto out_busy;
736:
737: rtc_status |= RTC_IS_OPEN;
...
740: spin_unlock_irq(&rtc_lock);
741: return 0;
742:
743: out_busy:
744: spin_unlock_irq(&rtc_lock);
745: return -EBUSY;
746: }
linux-3.18.1/drivers/char/rtc.c
753: static int rtc_release(struct inode *inode, struct file *file)
754: {
...
766: spin_lock_irq(&rtc_lock);
...
784: spin_lock_irq(&rtc_lock);
785: rtc_irq_data = 0;
786: rtc_status &= ~RTC_IS_OPEN;
787: spin_unlock_irq(&rtc_lock);
788:
789: return 0;
790: }
rtc_open() で立てた rtc_status の RTC_IS_OPEN ビットは、close() システ
ム・コールで呼ばれる rtc_release() ( struct file_operations rtc_fops の
.release) で、落とされる。
linux-3.18.1/drivers/char/rtc.c
718: static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
719: {
720: long ret;
721: ret = rtc_do_ioctl(cmd, arg, 0);
722: return ret;
723: }
398: static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
399: {
400: struct rtc_time wtime;
...
418: switch (cmd) {
...
480: case RTC_ALM_READ: /* Read the present alarm time */
...
491: case RTC_ALM_SET: /* Store a time into the alarm */
...
540: case RTC_RD_TIME: /* Read the time/date from RTC */
541: {
542: memset(&wtime, 0, sizeof(struct rtc_time));
543: rtc_get_rtc_time(&wtime);
544: break;
545: }
546: case RTC_SET_TIME: /* Set the RTC */
547: {
548: struct rtc_time rtc_tm;
549: unsigned char mon, day, hrs, min, sec, leap_yr;
550: unsigned char save_control, save_freq_select;
551: unsigned int yrs;
...
559: if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
560: sizeof(struct rtc_time)))
561: return -EFAULT;
562:
563: yrs = rtc_tm.tm_year + 1900;
564: mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
565: day = rtc_tm.tm_mday;
566: hrs = rtc_tm.tm_hour;
567: min = rtc_tm.tm_min;
568: sec = rtc_tm.tm_sec;
...
631: CMOS_WRITE(yrs, RTC_YEAR);
632: CMOS_WRITE(mon, RTC_MONTH);
633: CMOS_WRITE(day, RTC_DAY_OF_MONTH);
634: CMOS_WRITE(hrs, RTC_HOURS);
635: CMOS_WRITE(min, RTC_MINUTES);
636: CMOS_WRITE(sec, RTC_SECONDS);
637:
638: CMOS_WRITE(save_control, RTC_CONTROL);
639: CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
640:
641: spin_unlock_irq(&rtc_lock);
642: return 0;
643: }
711: default:
712: return -ENOTTY;
713: }
714: return copy_to_user((void __user *)arg,
715: &wtime, sizeof wtime) ? -EFAULT : 0;
716: }
カーネル空間とユーザ空間でデータをコピーする時には、次のような特殊な関 数を使う必要がある。
unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
これらの関数は、コピーの途中でページフォールトが発生した時にもうまくコ ピーできる(ページインの処理でプロセスがスリープすることがある)。また、 引数の番地が有効かどうかをチェックする。
Linux x86 アーキテクチャでは、カーネル空間とユーザ空間が一部重なってい ることがある。この場合、カーネルでmemcpy() を使ったり、直接ポインタを操 作してもユーザ空間がアクセスできてしまうが、それは誤りである。
linux-3.18.1/drivers/char/rtc.c
1294: static void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1295: {
1296: unsigned long uip_watchdog = jiffies, flags;
1297: unsigned char ctrl;
...
1322: spin_lock_irqsave(&rtc_lock, flags);
1323: rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1324: rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1325: rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1326: rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1327: rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1328: rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1329: /* Only set from 2.6.16 onwards */
1330: rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
...
1335: ctrl = CMOS_READ(RTC_CONTROL);
1336: spin_unlock_irqrestore(&rtc_lock, flags);
...
1356: rtc_tm->tm_year += epoch - 1900;
1357: if (rtc_tm->tm_year <= 69)
1358: rtc_tm->tm_year += 100;
1359:
1360: rtc_tm->tm_mon--;
1361: }
void outb(unsigned char value, unsigned short port) ポート番号 port に 1 バイトの value を出力する unsigned char inb(unsigned short port) ポート番号 port から 1 バイトの value を入力してその値を返す1 バイト 8 ビットではなくて 2 バイト 16 ビット 単位のもの (inw(), outw()) や4 バイト 32 ビット単位のもの( inl(), outl() ) もある。
linux-3.18.1/arch/x86/include/asm/mc146818rtc.h
12: #define RTC_PORT(x) (0x70 + (x))
...
93: #define CMOS_READ(addr) rtc_cmos_read(addr)
94: #define CMOS_WRITE(val, addr) rtc_cmos_write(val, addr)
linux-3.18.1/arch/x86/kernel/rtc.c
116: unsigned char rtc_cmos_read(unsigned char addr)
117: {
118: unsigned char val;
119:
120: lock_cmos_prefix(addr);
121: outb(addr, RTC_PORT(0));
122: val = inb(RTC_PORT(1));
123: lock_cmos_suffix(addr);
124:
125: return val;
126: }
...
129: void rtc_cmos_write(unsigned char val, unsigned char addr)
130: {
131: lock_cmos_prefix(addr);
132: outb(addr, RTC_PORT(0));
133: outb(val, RTC_PORT(1));
134: lock_cmos_suffix(addr);
135: }
linux-3.18.1/include/linux/mc146818rtc.h
47: #define RTC_SECONDS 0
48: #define RTC_SECONDS_ALARM 1
49: #define RTC_MINUTES 2
50: #define RTC_MINUTES_ALARM 3
51: #define RTC_HOURS 4
52: #define RTC_HOURS_ALARM 5
...
56: #define RTC_DAY_OF_WEEK 6
57: #define RTC_DAY_OF_MONTH 7
58: #define RTC_MONTH 8
59: #define RTC_YEAR 9
...
63: #define RTC_REG_A 10
64: #define RTC_REG_B 11
...
90: #define RTC_CONTROL RTC_REG_B
linux-3.18.1/arch/x86/boot/boot.h
43: static inline void outb(u8 v, u16 port)
44: {
45: asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
46: }
47: static inline u8 inb(u16 port)
48: {
49: u8 v;
50: asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
51: return v;
52: }
asm ( "アセンブラの命令列" : 出力オペランド(省略可) : 入力オペランド(省略可) : 破壊するレジスタ(省略可) )
"制約"(Cの値)
= があると、書き込み専用になる。
linux-3.18.1/arch/x86/include/asm/io.h
269: #define BUILDIO(bwl, bw, type) \
270: static inline void out##bwl(unsigned type value, int port) \
271: { \
272: asm volatile("out" #bwl " %" #bw "0, %w1" \
273: : : "a"(value), "Nd"(port)); \
274: } \
275: \
276: static inline unsigned type in##bwl(int port) \
277: { \
278: unsigned type value; \
279: asm volatile("in" #bwl " %w1, %" #bw "0" \
280: : "=a"(value) : "Nd"(port)); \
281: return value; \
282: } \
309: BUILDIO(b, b, char)
310: BUILDIO(w, w, short)
311: BUILDIO(l, , int)
id1##id2」 は、
識別子(関数名、変数名等)の結合を意味する。
たとえば、マクロ定義の引数 bwl が値 b を持っていれば、out##bwl は、outb となる。
(「#」がなければ、「out bwl」 は、「out b」と間に空白が残る)。
#var」 は、文字列化。
たとえば、マクロ定義の引数 bwl が値 b を持っていれば、#bwl は、"b" となる。
(「#」がなければ、bwl は、b )。
static inline unsigned char inb(int port) {
unsigned char value;
asm volatile("inb %w1, %b0" : "=a"(value) : "Nd"(port));
return value;
}
static inline void outb(unsigned char value, int port) {
asm volatile("outb %b0, %w1" : : "a"(value), "Nd"(port));
}
$ cat /proc/ioports
0000-001f : dma1
0020-0021 : pic1
0040-0043 : timer0
0050-0053 : timer1
0060-0060 : keyboard
0064-0064 : keyboard
0070-0077 : rtc
0080-008f : dma page reg
...
ff80-ff9f : 0000:00:1d.0
ff80-ff9f : uhci_hcd
$
図? x86 の Intel 8259
図? x86 の APIC
例:
例:
図? 割り込み記述子テーブルと割り込みハンドラ
typedef void (*funcp_t)(void); funcp_t idt[256];
old_pc = pc; old_flags = flags;
n = 割り込みベクタ; handler = idt[n];
push old_pc; push old_flags; pc = handler; // (*handler)();ただし、単純な call 命令とは違い、スタック上にプログラムカウンタの他に、 プロセッサの状態を示すフラグ等も積む。
図? PICの線が不足した時の対応
include/linux/interrupt.h
typedef irqreturn_t (*irq_handler_t)(int, void *);
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
const char *name, void *dev)
void free_irq(unsigned int, void *dev)
irqreturn_t irq_handler_t(int irq, void *dev)
$ cat /proc/interrupts
CPU0 CPU1
0: 4208761 38584 IO-APIC-edge timer
1: 0 3 IO-APIC-edge i8042
7: 0 0 IO-APIC-edge parport0
8: 1 2 IO-APIC-edge rtc
9: 0 0 IO-APIC-level acpi
12: 3 1 IO-APIC-edge i8042
50: 5380 86508 PCI-MSI ahci
74: 346 0 PCI-MSI HDA Intel
98: 294 28232 PCI-MSI eth1
169: 130 57006 IO-APIC-level uhci_hcd:usb3
177: 0 0 IO-APIC-level uhci_hcd:usb4, uhci_hcd:usb7
217: 358 149530 IO-APIC-level ehci_hcd:usb1, uhci_hcd:usb5
225: 0 0 IO-APIC-level ehci_hcd:usb2, uhci_hcd:usb6
233: 0 0 IO-APIC-level uhci_hcd:usb8
NMI: 0 0
LOC: 4246864 4246863
ERR: 0
MIS: 0
$
linux-3.18.1/include/linux/rtc.h
69: typedef struct rtc_task {
70: void (*func)(void *private_data);
71: void *private_data;
72: } rtc_task_t;
linux-3.18.1/drivers/char/rtc.c
96: static unsigned long rtc_port;
97: static int rtc_irq;
...
191: static unsigned long rtc_status; /* bitmapped status byte. */
192: static unsigned long rtc_freq; /* Current periodic IRQ rate */
193: static unsigned long rtc_irq_data; /* our output to the world */
...
200: static DEFINE_SPINLOCK(rtc_task_lock);
201: static rtc_task_t *rtc_callback;
...
953: static int __init rtc_init(void)
954: {
...
1000: if (request_irq(rtc_irq, rtc_interrupt, IRQF_SHARED, "rtc",
1001: (void *)&rtc_port)) {
1002: rtc_has_irq = 0;
1003: printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
1004: return -EIO;
1005: }
...
1136: }
239: static irqreturn_t rtc_interrupt(int irq, void *dev_id)
240: {
...
248: spin_lock(&rtc_lock);
249: rtc_irq_data += 0x100;
250: rtc_irq_data &= ~0xff;
...
259: rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
...
262: if (rtc_status & RTC_TIMER_ON)
263: mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
264:
265: spin_unlock(&rtc_lock);
...
268: spin_lock(&rtc_task_lock);
269: if (rtc_callback)
270: rtc_callback->func(rtc_callback->private_data);
271: spin_unlock(&rtc_task_lock);
...
276: return IRQ_HANDLED;
277: }
linux-3.18.1/arch/x86/include/asm/irq_vectors.h
49: #define IA32_SYSCALL_VECTOR 0x80
...
127: #define NR_VECTORS 256
linux-3.18.1/arch/x86/kernel/traps.c
79: gate_desc idt_table[NR_VECTORS] __page_aligned_bss;
...
791: void __init trap_init(void)
792: {
...
803: set_intr_gate(X86_TRAP_DE, divide_error);
804: set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
...
838: set_system_trap_gate(SYSCALL_VECTOR, &system_call);
...
862: }
linux-3.18.1/kernel/irq/handle.c
183: irqreturn_t handle_irq_event(struct irq_desc *desc)
184: {
185: struct irqaction *action = desc->action;
186: irqreturn_t ret;
187:
188: desc->istate &= ~IRQS_PENDING;
189: irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
...
192: ret = handle_irq_event_percpu(desc, action);
...
195: irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
196: return ret;
197: }
linux-3.18.1/kernel/irq/handle.c
133: irqreturn_t
134: handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
135: {
136: irqreturn_t retval = IRQ_NONE;
137: unsigned int flags = 0, irq = desc->irq_data.irq;
138:
139: do {
140: irqreturn_t res;
...
143: res = action->handler(irq, action->dev_id);
...
150: switch (res) {
...
164: case IRQ_HANDLED:
165: flags |= action->flags;
166: break;
167:
168: default:
169: break;
170: }
171:
172: retval |= res;
173: action = action->next;
174: } while (action);
...
180: return retval;
181: }
struct irqaction *action は、
action->next でリスト構造を作っている。
プロセス・コンテキストでできること。
割り込みコンテキストでは、このうようなことはできない。 速やかに終了すべきである。busy loop はできるが、あまり やらない方がよい。他の割り込みは、実行される可能性もある。
unsigned long flags; local_irq_save(flags); /* 割り込み禁止 */ ... local_irq_restore(flags); /* 割り込み許可 (save の時の状態にもどる) */単一CPUの x86 では、cli() と sti() で割り込みの禁止と許可を設定する方法 があった。それそれ同名の CPU の命令を実行して、全ての割り込みを禁止/許 可する。マルチプロセッサ(マルチコア含む)では、1つのCPU で割り込みを禁止 しても、他の CPU では許可されていることがあるので、cli()/sti() の方法は 使えない。
特定の割り込み番号の割り込みを禁止する方法もある。
void disable_irq(unsigned ing irq);
// 全CPUの割り込みを禁止する
void disable_irq_nosync(unsigned ing irq);
// 同上。ただし、割り込みハンドラの終了を待たない。
void enable_irq(unsigned ing irq);
// 割り込みを許可する。
void synchronize_irq(unsigned ing irq);
// 割り込みハンドラの終了を待つ。
memcpy( /*空欄(a)*/,/*空欄(b)*/,/*空欄(c)*/ );
return 0;
なお、memcpy() のインタフェースは、次のようになっている。
void * memcpy(void *destination, const void *source, size_t len);sourceは、コピー元、destination は、コピー先、len は長さ(バイト数)であ る。結果として destination を返す。
C言語の 3 項演算子(?と:)は、次のような意味である。
条件 ? 式1 : 式2
「条件」が成り立つ(非0)なら、「式1」の値、成り立たな
ければ、「式2」の値になる。この課題では、「間違ったプログラム」
を書く課題であり、return 0;と常に 0 (成功) を返すようにしている。
unsigned char hh;
outb( /*空欄(a)*/, 0x70 );
hh = inb( /*空欄(b)*/ );