コグノスケ


link 未来から過去へ表示(*)  link 過去から未来へ表示

link もっと前
2008年3月10日 >>> 2008年3月10日
link もっと後

2008年3月10日

かーねる

以前、hdk神からLinuxカーネルは最適化を有効にしないとコンパイルできないという話を聞いたのですが、詳解Linuxカーネルをパラパラと読んでいたら理由が書いてありました。

この不思議な現象を生み出す原因はfix_to_virt関数にあります。どのアーキテクチャにもあると思います。ざっと確認した限りではmips, sh, x86_64, sparc, i386, umに同様の関数が定義されていました(ファイル名はasm/fixmap.h)。
例えばmipsのコードを見ると、以下のようになっています。

fix_to_virt関数(linux kernel 2.6.23.8)

static inline unsigned long fix_to_virt(const unsigned int idx)
{
        /*
         * this branch gets completely eliminated after inlining,
         * except when someone tries to use fixaddr indices in an
         * illegal way. (such as mixing up address types or using
         * out-of-range indices).
         *
         * If it doesn't get removed, the linker will complain
         * loudly with a reasonably clear error message..
         */
        if (idx >= __end_of_fixed_addresses)
                __this_fixmap_does_not_exist();

        return __fix_to_virt(idx);
}

引数idxには必ず __end_of_fixed_addresses未満の定数を渡します。するとこのコードが最適化される際にifの条件が評価され、if文が常に成立しないことがわかります。コンパイラはifブロックを全て消しさってから、呼び出し元の関数に埋め込み(インライン展開)ます。

__end_of_fixed_addressesってのは、各アーキテクチャの固定メモリマップのアドレスを定義するfixed_addresses列挙型の最後に置かれる定数です。値そのものより「どの固定メモリマップアドレスよりも大きい」ことが重要です。この値を越えたアドレスへのアクセスは不正なアクセスなのです。
mipsでは以下のような列挙型で定義されています。

idxに渡すfixed_addresses列挙型

enum fixed_addresses {
#define FIX_N_COLOURS 8
        FIX_CMAP_BEGIN,
#ifdef CONFIG_MIPS_MT_SMTC
        FIX_CMAP_END = FIX_CMAP_BEGIN + (FIX_N_COLOURS * NR_CPUS),
#else
        FIX_CMAP_END = FIX_CMAP_BEGIN + FIX_N_COLOURS,
#endif
#ifdef CONFIG_HIGHMEM
        /* reserved pte's for temporary kernel mappings */
        FIX_KMAP_BEGIN = FIX_CMAP_END + 1,
        FIX_KMAP_END = FIX_KMAP_BEGIN+(KM_TYPE_NR*NR_CPUS)-1,
#endif
        __end_of_fixed_addresses
};

正しくない引数(変数や __end_of_fixed_addresses以上の定数)を渡す呼び出しがあると、if文が消去できず __this_fixmap_does_not_exist関数の呼び出しが残ります。しかしこの関数はどこにも定義されていないため、リンク時にエラーになるのです。

つまりこのif文は、最適化によって消去されることを前提に作ってあります。逆に言うと、最適化しなければ正しい呼び出しでも __this_fixmap_does_not_exist関数の呼び出しが残ってしまってコンパイルが通らないことを意味しています。

こうすることで実行時のチェックを省略できて速くなるのでしょうけど、gccの挙動にべったり依存しています。この技を使わずとも実現できる何か良い方法は無かったのかなあ…。

編集者:すずき(2008/03/11 21:18)

コメント一覧

  • コメントはありません。
open/close この記事にコメントする



link もっと前
2008年3月10日 >>> 2008年3月10日
link もっと後

管理用メニュー

link 記事を新規作成

<2008>
<<<03>>>
------1
2345678
9101112131415
16171819202122
23242526272829
3031-----

最近のコメント5件

  • link 24年4月22日
    hdkさん (04/24 08:36)
    「うちのHHFZ4310は15年突破しまし...」
  • link 24年4月22日
    すずきさん (04/24 00:37)
    「ちゃんと数えてないですけど蛍光管が10年...」
  • link 24年4月22日
    hdkさん (04/23 20:52)
    「おお... うちのHHFZ4310より後...」
  • link 20年6月19日
    すずきさん (04/06 22:54)
    「ディレクトリを予め作成しておけば良いです...」
  • link 20年6月19日
    斎藤さん (04/06 16:25)
    「「Preferencesというメニューか...」

最近の記事3件

  • link 24年4月25日
    すずき (04/26 16:49)
    「[AVIFの変換] AVIFが読めないアプリケーションがたまにあるので、AVIF(AV1 Image File Format)...」
  • link 24年2月7日
    すずき (04/24 02:52)
    「[複数の音声ファイルのラウドネスを統一したい] PCやデジタル音楽プレーヤーで音楽を聞いていると、曲によって音量の大小が激しく...」
  • link 24年4月22日
    すずき (04/23 20:13)
    「[仕事部屋の照明が壊れた] いきなり仕事部屋のシーリングライトが消えました。蛍光管の寿命にしては去年(2022年10月19日の...」
link もっとみる

こんてんつ

open/close wiki
open/close Linux JM
open/close Java API

過去の日記

open/close 2002年
open/close 2003年
open/close 2004年
open/close 2005年
open/close 2006年
open/close 2007年
open/close 2008年
open/close 2009年
open/close 2010年
open/close 2011年
open/close 2012年
open/close 2013年
open/close 2014年
open/close 2015年
open/close 2016年
open/close 2017年
open/close 2018年
open/close 2019年
open/close 2020年
open/close 2021年
open/close 2022年
open/close 2023年
open/close 2024年
open/close 過去日記について

その他の情報

open/close アクセス統計
open/close サーバ一覧
open/close サイトの情報

合計:  counter total
本日:  counter today

link About www.katsuster.net
RDFファイル RSS 1.0

最終更新: 04/26 16:49