{"action":"created","comment":{"url":"https://api.github.com/repos/intermezzOS/book/pulls/comments/66723292","id":66723292,"diff_hunk":"@@ -0,0 +1,186 @@\n+# Hello from Rust!\n+\n+Okay, time for the big finale: printing our `OKAY` from Rust. First, let's\n+change our `Makefile` to add the Rust code into our assembly code. We can build\n+on the steps we did earlier. Here's some new rules to add to the `Makefile`:\n+\n+```make\n+target/libcore:\n+        git clone http://github.com/intermezzos/libcore target/libcore\n+        cd target/libcore && git reset --hard 02e41cd5b925a1c878961042ecfb00470c68296b\n+\n+target/libcore/target/x86_64-unknown-intermezzos-gnu/libcore.rlib: target/libcore\n+        cp x86_64-unknown-intermezzos-gnu.json target/libcore\n+        cd target/libcore && cargo build --release --features disable_float --target=x86_64-unknown-intermezzos-gnu.json\n+\n+target/x86_64-unknown-intermezzos-gnu/release/libintermezzos.a: target/libcore/target/x86_64-unknown-intermezzos-gnu/libcore.rlib\n+        RUSTFLAGS=\"-L target/libcore/target/x86_64-unknown-intermezzos-gnu/release\" cargo build --release --target x86_64-unknown-intermezzos-gnu.json\n+```\n+\n+Whew! That's a bit of a mouthful. This is where it _might_ make some sense to\n+use some variables, at least. But let's not worry about this for now. We first\n+write a rule to download our `libcore`. Next, we write a rule to compile our\n+`libcore.rlib`. Finally, we write a rule to build `libintermezzos.a`. All of\n+these commands are ones we used earlier to build this stuff, so the details\n+shouldn't be completely new, though organizing them into these three rules\n+is.\n+\n+Try it out:\n+\n+```bash\n+$ make target/x86_64-unknown-intermezzos-gnu/release/libintermezzos.a\n+git clone http://github.com/intermezzos/libcore target/libcore\n+Cloning into 'target/libcore'...\n+remote: Counting objects: 140, done.\n+remote: Compressing objects: 100% (8/8), done.\n+remote: Total 140 (delta 3), reused 0 (delta 0), pack-reused 132\n+Receiving objects: 100% (140/140), 362.70 KiB | 120.00 KiB/s, done.\n+Resolving deltas: 100% (52/52), done.\n+Checking connectivity... done.\n+cd target/libcore && git reset --hard 02e41cd5b925a1c878961042ecfb00470c68296b\n+HEAD is now at 02e41cd Reintroduce panic == abort\n+cp x86_64-unknown-intermezzos-gnu.json target/libcore\n+cd target/libcore && cargo build --release --features disable_float --target=x86_64-unknown-intermezzos-gnu.json\n+   Compiling core v0.0.0 (file:///home/steve/src/intermezzOS/kernel/chapter_05/target/libcore)\n+RUSTFLAGS=\"-L target/libcore/target/x86_64-unknown-intermezzos-gnu/release\" cargo build --target x86_64-unknown-intermezzos-gnu.json\n+   Compiling intermezzos v0.1.0 (file:///home/steve/src/intermezzOS/kernel/chapter_05)\n+$\n+```\n+\n+Success! It should all build properly. There's one more thing I'd like to note\n+about this makefile: in a strict sense, it will try and rebuild too much. But\n+watch what happens if we try to build a second time:\n+\n+```bash\n+$ make target/x86_64-unknown-intermezzos-gnu/release/libintermezzos.a\n+cp x86_64-unknown-intermezzos-gnu.json target/libcore\n+cd target/libcore && cargo build --release --features disable_float --target=x86_64-unknown-intermezzos-gnu.json\n+RUSTFLAGS=\"-L target/libcore/target/x86_64-unknown-intermezzos-gnu/release\" cargo build --target x86_64-unknown-intermezzos-gnu.json\n+$\n+```\n+\n+We issued some commands, but didn't actually compile anything. With this\n+layout, we're letting Cargo worry if stuff needs to be rebuilt. This makes\n+our Makefile a bit easier to write, and also a bit more reliable. Cargo\n+knows what it needs to do, let's just trust it to do the right thing.\n+\n+Now that we have it building, we need to modify the rule that builds the kernel\n+to include `libintermezzos.a`:\n+\n+```make\n+target/kernel.bin: target/multiboot_header.o target/boot.o src/asm/linker.ld target/x86_64-unknown-intermezzos-gnu/release/libintermezzos.a\n+        ld -n -o target/kernel.bin -T src/asm/linker.ld target/multiboot_header.o target/boot.o target/x86_64-unknown-intermezzos-gnu/release/libintermezzos.a\n+```\n+\n+And then we can build:\n+\n+```bash\n+$ make\n+mkdir -p target\n+nasm -f elf64 src/asm/multiboot_header.asm -o target/multiboot_header.o\n+mkdir -p target\n+nasm -f elf64 src/asm/boot.asm -o target/boot.o\n+cp x86_64-unknown-intermezzos-gnu.json target/libcore\n+cd target/libcore && cargo build --release --features disable_float --target=x86_64-unknown-intermezzos-gnu.json\n+RUSTFLAGS=\"-L target/libcore/target/x86_64-unknown-intermezzos-gnu/release\" cargo build --release --target x86_64-unknown-intermezzos-gnu.json\n+ld -n -o target/kernel.bin -T src/asm/linker.ld target/multiboot_header.o target/boot.o target/x86_64-unknown-intermezzos-gnu/release/libintermezzos.a\n+$\n+```\n+\n+Hooray! We are now successfully building our assembly code and our Rust code, and then putting them together.\n+\n+Now, to write our Rust. Add this function to `src/lib.rs`:\n+\n+```rust\n+#[no_mangle]\n+pub extern fn kmain() -> ! {\n+\n+    loop { }\n+}\n+```\n+\n+This is our main function, which is traditionally called `kmain()`, for 'kernel\n+main.' We need to use the `#[no_mangle]` and `pub extern` annotations to indicate\n+that we're going to call this function like we would call a C function. The `-> !`\n+indicates that this function never returns. And in fact, it does not: the body\n+is an infinite `loop`.\n+\n+I'm going to pause here to mention that while I won't totally assume you're a\n+Rust expert, this is more of an OS tutorial than a Rust tutorial. If anything\n+about the Rust is confusing, I suggest you read over the [official book] to get\n+an actual introduction to the language. It's tough enough explaining operating\n+systems as it is without needing to fully explain a language too. But if you're\n+an experienced programmer, you might be able to get away without it.\n+\n+[official book]: http://doc.rust-lang.org/book\n+\n+Anyway, our `kmain()` doesn't do anything. But let's try calling it anyway.\n+Modfiy `src/asm/boot.asm`, removing all of the `long\\_mode\\_start` stuff,\n+and changing the `jmp` line in `start` to look like this:\n+\n+```x86asm\n+    ; jump to long mode!\n+    jmp gdt64.code:kmain\n+```\n+\n+Finally, add this line to the top of the file:\n+\n+```x86asm\n+extern kmain\n+```\n+\n+This line says that we'll be defining `kmain` elsewhere: in this case, in Rust!\n+And so we also change our `jmp` to jump to `kmain`.\n+\n+If you type `make run`, everything should compile and run, but then not display\n+anything. We didn't port over the message! Open `src/lib.rs` and change `kmain()`\n+to look like this:\n+\n+```rust\n+#[no_mangle]\n+pub extern fn kmain() -> ! {\n+    unsafe {\n+        let vga = 0xb8000 as *mut u64;\n+\n+        *vga = 0x2f592f412f4b2f4f;\n+    };\n+\n+    loop { }\n+}\n+```\n+\n+The first thing you'll notice is the `unsafe` annotation. Yes, while one of\n+Rust's defining features is safety, we'll certainly be making use of `unsafe`\n+in our kernel. However, we'll be using less than you think. While this is just\n+printing `OKAY` to the screen, our intermediate VGA driver will be using the\n+exact same amount, with a lot more safe code on top.\n+\n+In this case, the reason we need `unsafe` is the next two lines: we create a\n+pointer to `0xb8000`, and then write some numbers to it. Rust cannot know that\n+this is safe; if it did, it would have to understand that we are a kernel,\n+and understand the VGA specification. Having a programming langauge understand","path":"src/hello-from-rust.md","position":161,"original_position":161,"commit_id":"e08228eb25e5105a7fa3547af62dd40f5eab6ff3","original_commit_id":"e08228eb25e5105a7fa3547af62dd40f5eab6ff3","user":{"login":"guewen","id":417223,"avatar_url":"https://avatars.githubusercontent.com/u/417223?v=3","gravatar_id":"","url":"https://api.github.com/users/guewen","html_url":"https://github.com/guewen","followers_url":"https://api.github.com/users/guewen/followers","following_url":"https://api.github.com/users/guewen/following{/other_user}","gists_url":"https://api.github.com/users/guewen/gists{/gist_id}","starred_url":"https://api.github.com/users/guewen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guewen/subscriptions","organizations_url":"https://api.github.com/users/guewen/orgs","repos_url":"https://api.github.com/users/guewen/repos","events_url":"https://api.github.com/users/guewen/events{/privacy}","received_events_url":"https://api.github.com/users/guewen/received_events","type":"User","site_admin":false},"body":"s/langauge/language/ ","created_at":"2016-06-12T16:12:27Z","updated_at":"2016-06-12T16:12:27Z","html_url":"https://github.com/intermezzOS/book/pull/119#discussion_r66723292","pull_request_url":"https://api.github.com/repos/intermezzOS/book/pulls/119","_links":{"self":{"href":"https://api.github.com/repos/intermezzOS/book/pulls/comments/66723292"},"html":{"href":"https://github.com/intermezzOS/book/pull/119#discussion_r66723292"},"pull_request":{"href":"https://api.github.com/repos/intermezzOS/book/pulls/119"}}},"pull_request":{"url":"https://api.github.com/repos/intermezzOS/book/pulls/119","id":73493144,"html_url":"https://github.com/intermezzOS/book/pull/119","diff_url":"https://github.com/intermezzOS/book/pull/119.diff","patch_url":"https://github.com/intermezzOS/book/pull/119.patch","issue_url":"https://api.github.com/repos/intermezzOS/book/issues/119","number":119,"state":"open","locked":false,"title":"Set up rust","user":{"login":"steveklabnik","id":27786,"avatar_url":"https://avatars.githubusercontent.com/u/27786?v=3","gravatar_id":"","url":"https://api.github.com/users/steveklabnik","html_url":"https://github.com/steveklabnik","followers_url":"https://api.github.com/users/steveklabnik/followers","following_url":"https://api.github.com/users/steveklabnik/following{/other_user}","gists_url":"https://api.github.com/users/steveklabnik/gists{/gist_id}","starred_url":"https://api.github.com/users/steveklabnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/steveklabnik/subscriptions","organizations_url":"https://api.github.com/users/steveklabnik/orgs","repos_url":"https://api.github.com/users/steveklabnik/repos","events_url":"https://api.github.com/users/steveklabnik/events{/privacy}","received_events_url":"https://api.github.com/users/steveklabnik/received_events","type":"User","site_admin":false},"body":"Here's the next chapter! We finally have Rust code!","created_at":"2016-06-12T10:40:33Z","updated_at":"2016-06-12T16:12:27Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7b1622805d0a5e78aa8b17f1d1562d47f0b9de2c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/intermezzOS/book/pulls/119/commits","review_comments_url":"https://api.github.com/repos/intermezzOS/book/pulls/119/comments","review_comment_url":"https://api.github.com/repos/intermezzOS/book/pulls/comments{/number}","comments_url":"https://api.github.com/repos/intermezzOS/book/issues/119/comments","statuses_url":"https://api.github.com/repos/intermezzOS/book/statuses/e08228eb25e5105a7fa3547af62dd40f5eab6ff3","head":{"label":"intermezzOS:set-up-rust","ref":"set-up-rust","sha":"e08228eb25e5105a7fa3547af62dd40f5eab6ff3","user":{"login":"intermezzOS","id":16422108,"avatar_url":"https://avatars.githubusercontent.com/u/16422108?v=3","gravatar_id":"","url":"https://api.github.com/users/intermezzOS","html_url":"https://github.com/intermezzOS","followers_url":"https://api.github.com/users/intermezzOS/followers","following_url":"https://api.github.com/users/intermezzOS/following{/other_user}","gists_url":"https://api.github.com/users/intermezzOS/gists{/gist_id}","starred_url":"https://api.github.com/users/intermezzOS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intermezzOS/subscriptions","organizations_url":"https://api.github.com/users/intermezzOS/orgs","repos_url":"https://api.github.com/users/intermezzOS/repos","events_url":"https://api.github.com/users/intermezzOS/events{/privacy}","received_events_url":"https://api.github.com/users/intermezzOS/received_events","type":"Organization","site_admin":false},"repo":{"id":48530156,"name":"book","full_name":"intermezzOS/book","owner":{"login":"intermezzOS","id":16422108,"avatar_url":"https://avatars.githubusercontent.com/u/16422108?v=3","gravatar_id":"","url":"https://api.github.com/users/intermezzOS","html_url":"https://github.com/intermezzOS","followers_url":"https://api.github.com/users/intermezzOS/followers","following_url":"https://api.github.com/users/intermezzOS/following{/other_user}","gists_url":"https://api.github.com/users/intermezzOS/gists{/gist_id}","starred_url":"https://api.github.com/users/intermezzOS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intermezzOS/subscriptions","organizations_url":"https://api.github.com/users/intermezzOS/orgs","repos_url":"https://api.github.com/users/intermezzOS/repos","events_url":"https://api.github.com/users/intermezzOS/events{/privacy}","received_events_url":"https://api.github.com/users/intermezzOS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/intermezzOS/book","description":"The main docs for intermezzOS","fork":false,"url":"https://api.github.com/repos/intermezzOS/book","forks_url":"https://api.github.com/repos/intermezzOS/book/forks","keys_url":"https://api.github.com/repos/intermezzOS/book/keys{/key_id}","collaborators_url":"https://api.github.com/repos/intermezzOS/book/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/intermezzOS/book/teams","hooks_url":"https://api.github.com/repos/intermezzOS/book/hooks","issue_events_url":"https://api.github.com/repos/intermezzOS/book/issues/events{/number}","events_url":"https://api.github.com/repos/intermezzOS/book/events","assignees_url":"https://api.github.com/repos/intermezzOS/book/assignees{/user}","branches_url":"https://api.github.com/repos/intermezzOS/book/branches{/branch}","tags_url":"https://api.github.com/repos/intermezzOS/book/tags","blobs_url":"https://api.github.com/repos/intermezzOS/book/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/intermezzOS/book/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/intermezzOS/book/git/refs{/sha}","trees_url":"https://api.github.com/repos/intermezzOS/book/git/trees{/sha}","statuses_url":"https://api.github.com/repos/intermezzOS/book/statuses/{sha}","languages_url":"https://api.github.com/repos/intermezzOS/book/languages","stargazers_url":"https://api.github.com/repos/intermezzOS/book/stargazers","contributors_url":"https://api.github.com/repos/intermezzOS/book/contributors","subscribers_url":"https://api.github.com/repos/intermezzOS/book/subscribers","subscription_url":"https://api.github.com/repos/intermezzOS/book/subscription","commits_url":"https://api.github.com/repos/intermezzOS/book/commits{/sha}","git_commits_url":"https://api.github.com/repos/intermezzOS/book/git/commits{/sha}","comments_url":"https://api.github.com/repos/intermezzOS/book/comments{/number}","issue_comment_url":"https://api.github.com/repos/intermezzOS/book/issues/comments{/number}","contents_url":"https://api.github.com/repos/intermezzOS/book/contents/{+path}","compare_url":"https://api.github.com/repos/intermezzOS/book/compare/{base}...{head}","merges_url":"https://api.github.com/repos/intermezzOS/book/merges","archive_url":"https://api.github.com/repos/intermezzOS/book/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/intermezzOS/book/downloads","issues_url":"https://api.github.com/repos/intermezzOS/book/issues{/number}","pulls_url":"https://api.github.com/repos/intermezzOS/book/pulls{/number}","milestones_url":"https://api.github.com/repos/intermezzOS/book/milestones{/number}","notifications_url":"https://api.github.com/repos/intermezzOS/book/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/intermezzOS/book/labels{/name}","releases_url":"https://api.github.com/repos/intermezzOS/book/releases{/id}","deployments_url":"https://api.github.com/repos/intermezzOS/book/deployments","created_at":"2015-12-24T07:06:23Z","updated_at":"2016-06-12T12:21:43Z","pushed_at":"2016-06-12T12:21:08Z","git_url":"git://github.com/intermezzOS/book.git","ssh_url":"git@github.com:intermezzOS/book.git","clone_url":"https://github.com/intermezzOS/book.git","svn_url":"https://github.com/intermezzOS/book","homepage":"http://intermezzos.github.io/book","size":1505,"stargazers_count":163,"watchers_count":163,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":48,"mirror_url":null,"open_issues_count":15,"forks":48,"open_issues":15,"watchers":163,"default_branch":"master"}},"base":{"label":"intermezzOS:master","ref":"master","sha":"b247debe31af6ca19a5a32d534a15d7a5cfbe524","user":{"login":"intermezzOS","id":16422108,"avatar_url":"https://avatars.githubusercontent.com/u/16422108?v=3","gravatar_id":"","url":"https://api.github.com/users/intermezzOS","html_url":"https://github.com/intermezzOS","followers_url":"https://api.github.com/users/intermezzOS/followers","following_url":"https://api.github.com/users/intermezzOS/following{/other_user}","gists_url":"https://api.github.com/users/intermezzOS/gists{/gist_id}","starred_url":"https://api.github.com/users/intermezzOS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intermezzOS/subscriptions","organizations_url":"https://api.github.com/users/intermezzOS/orgs","repos_url":"https://api.github.com/users/intermezzOS/repos","events_url":"https://api.github.com/users/intermezzOS/events{/privacy}","received_events_url":"https://api.github.com/users/intermezzOS/received_events","type":"Organization","site_admin":false},"repo":{"id":48530156,"name":"book","full_name":"intermezzOS/book","owner":{"login":"intermezzOS","id":16422108,"avatar_url":"https://avatars.githubusercontent.com/u/16422108?v=3","gravatar_id":"","url":"https://api.github.com/users/intermezzOS","html_url":"https://github.com/intermezzOS","followers_url":"https://api.github.com/users/intermezzOS/followers","following_url":"https://api.github.com/users/intermezzOS/following{/other_user}","gists_url":"https://api.github.com/users/intermezzOS/gists{/gist_id}","starred_url":"https://api.github.com/users/intermezzOS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intermezzOS/subscriptions","organizations_url":"https://api.github.com/users/intermezzOS/orgs","repos_url":"https://api.github.com/users/intermezzOS/repos","events_url":"https://api.github.com/users/intermezzOS/events{/privacy}","received_events_url":"https://api.github.com/users/intermezzOS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/intermezzOS/book","description":"The main docs for intermezzOS","fork":false,"url":"https://api.github.com/repos/intermezzOS/book","forks_url":"https://api.github.com/repos/intermezzOS/book/forks","keys_url":"https://api.github.com/repos/intermezzOS/book/keys{/key_id}","collaborators_url":"https://api.github.com/repos/intermezzOS/book/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/intermezzOS/book/teams","hooks_url":"https://api.github.com/repos/intermezzOS/book/hooks","issue_events_url":"https://api.github.com/repos/intermezzOS/book/issues/events{/number}","events_url":"https://api.github.com/repos/intermezzOS/book/events","assignees_url":"https://api.github.com/repos/intermezzOS/book/assignees{/user}","branches_url":"https://api.github.com/repos/intermezzOS/book/branches{/branch}","tags_url":"https://api.github.com/repos/intermezzOS/book/tags","blobs_url":"https://api.github.com/repos/intermezzOS/book/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/intermezzOS/book/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/intermezzOS/book/git/refs{/sha}","trees_url":"https://api.github.com/repos/intermezzOS/book/git/trees{/sha}","statuses_url":"https://api.github.com/repos/intermezzOS/book/statuses/{sha}","languages_url":"https://api.github.com/repos/intermezzOS/book/languages","stargazers_url":"https://api.github.com/repos/intermezzOS/book/stargazers","contributors_url":"https://api.github.com/repos/intermezzOS/book/contributors","subscribers_url":"https://api.github.com/repos/intermezzOS/book/subscribers","subscription_url":"https://api.github.com/repos/intermezzOS/book/subscription","commits_url":"https://api.github.com/repos/intermezzOS/book/commits{/sha}","git_commits_url":"https://api.github.com/repos/intermezzOS/book/git/commits{/sha}","comments_url":"https://api.github.com/repos/intermezzOS/book/comments{/number}","issue_comment_url":"https://api.github.com/repos/intermezzOS/book/issues/comments{/number}","contents_url":"https://api.github.com/repos/intermezzOS/book/contents/{+path}","compare_url":"https://api.github.com/repos/intermezzOS/book/compare/{base}...{head}","merges_url":"https://api.github.com/repos/intermezzOS/book/merges","archive_url":"https://api.github.com/repos/intermezzOS/book/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/intermezzOS/book/downloads","issues_url":"https://api.github.com/repos/intermezzOS/book/issues{/number}","pulls_url":"https://api.github.com/repos/intermezzOS/book/pulls{/number}","milestones_url":"https://api.github.com/repos/intermezzOS/book/milestones{/number}","notifications_url":"https://api.github.com/repos/intermezzOS/book/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/intermezzOS/book/labels{/name}","releases_url":"https://api.github.com/repos/intermezzOS/book/releases{/id}","deployments_url":"https://api.github.com/repos/intermezzOS/book/deployments","created_at":"2015-12-24T07:06:23Z","updated_at":"2016-06-12T12:21:43Z","pushed_at":"2016-06-12T12:21:08Z","git_url":"git://github.com/intermezzOS/book.git","ssh_url":"git@github.com:intermezzOS/book.git","clone_url":"https://github.com/intermezzOS/book.git","svn_url":"https://github.com/intermezzOS/book","homepage":"http://intermezzos.github.io/book","size":1505,"stargazers_count":163,"watchers_count":163,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":48,"mirror_url":null,"open_issues_count":15,"forks":48,"open_issues":15,"watchers":163,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/intermezzOS/book/pulls/119"},"html":{"href":"https://github.com/intermezzOS/book/pull/119"},"issue":{"href":"https://api.github.com/repos/intermezzOS/book/issues/119"},"comments":{"href":"https://api.github.com/repos/intermezzOS/book/issues/119/comments"},"review_comments":{"href":"https://api.github.com/repos/intermezzOS/book/pulls/119/comments"},"review_comment":{"href":"https://api.github.com/repos/intermezzOS/book/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/intermezzOS/book/pulls/119/commits"},"statuses":{"href":"https://api.github.com/repos/intermezzOS/book/statuses/e08228eb25e5105a7fa3547af62dd40f5eab6ff3"}}}}
{"actor":{"display_login":"guewen"}}